(2011.07.11)读取字符串并计算总字符数.cpp

本文展示了一个使用C++编写的简单程序,该程序能够读取用户输入的字符串,并计算这些字符串中的总字符数。通过循环读取每行输入直到用户输入空行为止,程序会显示每一行的字符数量及累计总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 读取字符串并计算总字符数.cpp
/*******************************************************************
程序的输出:
Enter a line:
nice pants
"nice pants" contains 9 characters
9 characters total
Enter next line(empty line to quit):
thanks
"thanks"contains 6 characters
15 characters total
Enter next line(empty line to quit):
parting is such sweet sorrow
"parting i" contains 9 characters
24 characters total
Enter next line(empty line to quit):
ok
"ok" contains 2 charsacters
26 characters total
Enter next line (empty line to quit):


Bye
*******************************************************************/


#include <iostream>
using namespace std;


inline int countTotal(const char* t, int n)
{
int i = 0;
while(t[i] != '\0')
{
i++;
++n;
}
return n;
}


int main()
{
cout << "Enter a line:" << endl;
char temp[10];
int n = 0;
int countTotal(const char*, int n);
cin.getline(temp, 10);
while(temp[0])
{
cin.clear();
n = countTotal(temp, n);
cout << "\"" << temp << "\"" << " contains " 
<< sizeof(temp) - 1 << " characters ";
cout << endl << n << " characters total" << endl
    << "Enter next line (empty line to quit):" << endl;
cin.getline(temp, 10);
}
cout << "Bye" << endl;
return 0;

}


//还是有很多问题,太晚了,先保存起来,明天再作修改。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值