Case 1 : Enter an array or string : 1020304050 Length of the string is : 10 Case 2 : Enter an array or string : hello Length of the string is : 5 Case 3: Enter an array or string : Program25 Length of the string is : 9
Users Online
· Guests Online: 123
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C++ Program to Find the Length of a String
C++ Program to Find the Length of a String
This is a C++ Program to Find the Length of a String/Array.
Problem Description
The program takes a string or an array and prints its length.
Problem Solution
1. The program takes a string or an array.
2. Using inbuilt function, the length of the string is found.
3. The result is printed.
4. Exit.
C++ Program/Source code
Here is the source code of C++ Program to Find the Length of a String/Array. The program output is shown below.
-
#include<iostream>
-
#include<string.h>
-
using namespace std;
-
int main ()
-
{
-
char str[50];
-
int len;
-
cout << "Enter an array or string : ";
-
gets(str);
-
len = strlen(str);
-
cout << "Length of the string is : " << len;
-
return 0;
-
}
Program Explanation
1. The user is asked to enter an array or a string and it is stored in the character variable ‘str’.
2. Using the function strlen() which is inbuilt under the library ‘string.h’, length of the string is found.
3. The result is then printed.
Runtime Test Cases
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.