您的位置:首页 > 编程语言 > C语言/C++

C++primer plus第六版课后编程练习答案9.2

2015-11-29 17:14 567 查看
#include<iostream>
#include<string>

using namespace std;

void strcount(const string str);

int main()
{
string input;

cout<<"Enter a line:\n";
getline(cin,input);
while(cin)
{
if(""==input)
break;
strcount(input);
cout<<"Enter next line(empty line to quit):\n";
getline(cin,input);
}
cout<<"Bye\n";
return 0;
}

void strcount(const string str)
{
static int total=0;
int count=0;

cout<<"\""<<str<<"\" contains ";
for(int i=0;i<str.length();i++)
{
if(str[i]!=' ')
count++;
}
total+=count;

cout<<count<<" characters\n";
cout<<total<<" characters total\n";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: