Q-22: Write a C/C++ program to find the length of a given string.
    

Using - C


#include<stdio.h> void main() { char str[1024]; int i=0, length=0; printf("Enter the String: "); gets(str); while(str[i] != '\0') { length++; i++; } printf("Length of the given string: %d",length); }

OUTPUT

Enter the String:  programmer OP!
Length of the given string:  14
        

Using - C++


#include<iostream> using namespace std; int main() { string str; int i=0, length=0; cout<< "Enter the String: "; getline(cin,str); while(str[i] != '\0') { length++; i++; } cout<< "Length of the given string: "<< length; return 0; }

OUTPUT

Enter the String:  welcome to GCCxCSD
Length of the given string:  18