C++版本:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main(void) {
string line;
int count = 0;
int length = 0;
cout << "请输入任意多行单词:\n";
while (1) {
// 遇到文件结束符时, 返回 NULL(0)
if ((cin >> line).eof()) {
break;
}
count++;
length += line.length();
}
cout << "一共有" << count << "行" << endl;
cout << "共有 " << length << "个字符"<<endl;
system("pause");
return 0;
}
C语言版本:
#include <stdio.h>
#include <string.h>
#include <Windows.h>
int main(void) {
char line[1024];
int count = 0;
int length = 0;
printf( "请输入任意多行单词:\n");
while (1) {
// 遇到文件结束符时, 返回 NULL(0)
if (gets_s(line) == 0) {
break;
}
count++;
length += strlen(line);
}
printf( "一共有 %d行\n" ,count);
printf( "共有 %d个字符 " ,length );
system("pause");
return 0;
}