Write a C++ program to find the length of string.
Note: In this program, to enter string you can also use another function like cin.getline(string_name, string_size) in place of cin.get(string_name, string_size)
#include<iostream>
using namespace std;
int main( )
{
int length = 0; // variable to hold length of string
int i; // loop variable to count character in the string
char name[30];
cout<<"Enter string to find the length is : ";
cin.get(name, 30);
for(i = 0; name[i] != '\0'; i++)
{
length = length + 1;
}
cout << "Length of string is: " << i;
return 0;
}
Program output is tested on www.jdoodle.com
Enter string to find the length is : Mukesh Kumar Rajput
Length of string is: 19
Thanks
Mukesh Rajput
Post A Comment:
0 comments: