#include <iostream>
#include <string>
using namespace std;
int main(void){
char line[518];
int i = 0;//数组下标
int count = 0;//单词计数
cout << "请输入单词:";
gets_s(line,sizeof(line));
while(line[i] == ' ') i++;
while(line[i]){
while(line[i] && line[i] !=' ') i++;
while(line[i] == ' ') i++;
count ++;
}
cout << "一共有" << count << "个单词" << endl;
system ("pause");
return 0;
}
该C++代码段接收用户输入的一行文本,通过处理字符串中的空格来计数单词数量。它使用了标准库iostream和string,并通过gets_s函数安全地读取用户输入,然后遍历字符串以计算单词计数。
2万+

被折叠的 条评论
为什么被折叠?



