首页 > 学院 > 开发设计 > 正文

题目1182:统计单词

2019-11-06 08:26:51
字体:
来源:转载
供稿:网友

PRoblem:

Solution:

这道题的思路其实是很简单的,但由于我对c++基本语法的不清楚导致耗费了很久。在用cin读取输入字符串时,遇到空白就会停止了,比如说输入为“hello world”读进去的只有“hello”,只能cin<<hello<<world这样读。 如果我们希望最终得到的字符串中保留空白符,可以用getline()来读取,getline函数的参数为一个输入流和一个string对象,从给定输入流中读取内容,直到遇到换行符才会停止,如getline(cin,line)
#include <iostream>#include <stdio.h>#include <string>using namespace std;int main(){    string input;    while (cin >> input )    {        int count = 0;        int i;        for (i = 0;input[i]!='/0' && input[i]!='.';i++)        {            count++;        }        if (input[i] == '.') cout<<count<<endl;        else cout<<count<<' ';    }    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表