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: "<