write a program to find the length of string



Here we have to calculate the length of string means how many characters are contained by the string. The code is as follows:

#include<stdio.h>
#include<iostream.h>
#include<conio.h>

void main()
{
int i,count=0;
char ch[20];
clrscr();
cout <<"Enter Any String: ";
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
count++;
}
cout <<"Length of String: "<<count;
getch();
}

#include<iostream.h> used for input/output of data
#include<conio.h> provides several functions like clrscr(), getch() etc.
After that in main() function we have declared a variables i, count=0; and one character array ch[20]. after that we have used clrscr(); function to clear the scree when program will run. It will clear the previous output from any program. Now we have used the cout function that shows message “Enter Any Stirng :”; and when we enter any string then this string is read by gets() function present in “stdio.h” header file. After that we have to check the condition in the for loop that is ch[i]!='\0' its means loop perform until string is not null, when string is null loop will be terminate. And inside the loop we have used count variable and it will always be incremented by one whenever loop will be continued. When the loop will terminate then count has some value is the length of the string.
The output is as follows:

c++ program