C++ Program to Find String Length

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;
}
C++ Program to Find String Length 1

In this C++ 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

C++ 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 C++ 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