Write a C++ Program to Find String Length with an example. This programming language has a length function to find the string characters.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string txt;
cout << "\nPlease Enter the String to find Length = ";
getline(cin, txt);
int len = txt.length();
cout<< "\nThe Length of '" << txt << "' String = " << len;
return 0;
}

In this language, there is a size function that returns the length of a string. Please refer to C++ programs.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string txt;
cout << "\nPlease Enter the Text = ";
getline(cin, txt);
int len = txt.size();
cout<< "\nThe Length of '" << txt << "' = " << len;
return 0;
}
Please Enter the Text = hello world
The Length of 'hello world' = 11
Program to Find String Length using a While loop
#include<iostream>
#include<string>
using namespace std;
int main()
{
string txt;
cout << "\nPlease Enter the Text = ";
getline(cin, txt);
int i = 0;
while(txt[i])
{
i++;
}
cout<< "\nThe Length of '" << txt << "' = " << i;
return 0;
}
Please Enter the Text = c++ programming
The Length of 'c++ programming' = 15
This Program Finds the total number of String characters using For loop.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string txt;
int i;
cout << "\nPlease Enter the text = ";
getline(cin, txt);
for (i = 0; txt[i]; i++);
cout<< "\nThe Length of " << txt << " = " << i;
return 0;
}
Please Enter the Text = hello world!
The Length of 'hello world!' = 12