C++方法
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main(void){
string line;
int lineCount = 0;
int lineLength = 0;
cout << "请输入文本:" << endl;
while(1){
if(getline(cin,line) ==0){
break;
}
lineCount ++;
//会读取空格
lineLength += line.length();
}
cout << "一共有" << lineCount << "行" << endl;
cout << "一共有" << lineLength << "字符" << endl;
system("pause");
return 0;
}
C语言
#include <stdio.h>
#include <Windows.h>
#include <string.h>
int main(void){
char line[2480];
int lineCount = 0;
int lineLength = 0;
printf( "请输入文本:\n");
while(1){
if(gets(line) ==0){
break;
}
lineCount ++;
//会读取空格
lineLength += strlen(line);
}
printf( "一共有%d行\n",lineCount) ;
printf( "一共有%d个字符\n",lineLength );
system("pause");
return 0;
}