分别使用C / C++实现
C++方法:
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main(void){
string word;
int count = 0;
int length =0;
cout << "请输入任意单词:" << endl;
while(1){
//判断何时结束输入,遇到EOF文件结束符(Ctrl+Z)时,跳出循环
if((cin >> word == 0)){
break;
}
count ++;
length += word.length();
}
cout << "一共输入" << count << "个单词" << endl;
cout << "单词的总长度为:" << length << endl;
system("pause");
return 0;
}
C语言方法实现
#include <Windows.h>
#include <string.h>
#include <stdio.h>
int main(void){
char word [128];
int count = 0;
int length =0;
printf( "请输入任意单词:\n" );
while(1){
if((scanf("%s",word) == -1)){
break;
}
count ++;
length += strlen(word);
}
printf( "一共输入%d个单词\n",count) ;
printf("单词的总长度为:%d\n",length );
system("pause");
return 0;
}