【发布时间】:2013-12-09 11:01:05
【问题描述】:
我有以下代码可以获取并打印一个字符串。
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int main()
{
string str;
cout << "Enter a string: ";
getline(cin, str);
cout << str;
getch();
return 0;
}
但是如何使用strlen()函数统计这个字符串中的字符数呢?
【问题讨论】:
-
str.size()怎么样? -
main必须返回int。 -
为什么是
strlen? -
所以我们不能使用 strlen() 函数计算字符串中的字符数?
-
标题?作者?它显然是由不太了解该语言的人编写的,并且可能包含更严重的错误。我想警告人们远离它。