下学期第二周实验报告3(1)
2012-03-04 11:02
190 查看
实验目的:输入一行字符,统计其中有多少个单词。
项目代码:
实验截图:
项目代码:
#include <iostream> #include "string.h" using namespace std; int main() { char str[200]; cin.get(str, 100, '\n'); for(int i = 0, j = 0; str[i] != '\0'; i++) { j++; } for(int x = 0, y = 0; x <= j; x++) { if(str[x] == ' ' &&str[x + 1] != ' ') { y++; } } cout << "There are " << y + 1 << " words in the line." << endl; return 0; }
实验截图: