C++ 程序查找字符串长度

编写一个 C++ 程序,通过示例查找字符串长度。此编程语言有一个用于查找字符串字符的 length 函数。

#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;
}
Program to Find String Length

在此语言中,有一个 size 函数返回字符串的长度。请参阅 C++ 程序

#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

使用 While 循环查找字符串长度的程序

#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

此程序使用 For 循环查找字符串字符的总数。

#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