Letters and Words
|
题目描述
Calculate the number of letters (including white spaces but excluding line breaks) and the number of words in the given paragraph. The input consists of only alphabets and white spaces.
样例输入
Hello world Welcome to SYSU a a a
样例输出
Letters: 33 Words: 8
提示
Use cin.get() to read the paragraph. Use istringstream strm(string) to obtain a string stream. The string stream can help you to read all string blocks in a simple way. |
没什么好介绍的,一道作业题。
#include
#include
#include
using namespace std;
int main()
{
int word=0,letters=0;
string str, line;
while(getline(cin, line))//read the paragraph.
{
for(int i=0;i<1000;i++){
if(line[i]=='\0')break;
letters++;//calculate the number of letters.
}
istringstream stream(line);//istringstream the line.
while(stream>>str) word++;//calculate the number of word.
}
cout<<"Letters: "<
<