/*
Enter a line:
get out
" get out " contains 7 characters
7 characters total
Enter next line (empty line to quit):
leave me, please.
" leave me, please. " contains 17 characters
24 characters total
*/
#include <iostream>
#include <string>
void strcount(const std::string * str);
int main() {
using namespace std;
string input;
cout << "Enter a line:\n";
getline(cin, input);
while (input != "") {
strcount(&input);
cout << "Enter next line (empty line to quit):\n";
getline(cin, input);
}
}
void strcount(const std::string * str) {
using namespace std;
static int total = 0;
int count = 0;
int i = 0;
cout << "\" " << *str << " \" contains ";
while ((*str)[i++])
count++;
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
c++ primer plus 第九章《编程题9.6.2》
最新推荐文章于 2020-02-09 17:22:06 发布
本文介绍了一个简单的C++程序,该程序能够逐行读取用户输入的字符串,并统计每行及累计的字符数量。通过使用标准输入输出库和字符串处理功能,程序展示了基本的输入输出操作及字符串长度计算。
511

被折叠的 条评论
为什么被折叠?



