C++ Primer 习题3.10

本博客提供了一个C++程序示例,用于从输入字符串中移除所有标点符号,并输出处理后的字符串。程序通过cin获取用户输入,使用is_punct函数判断字符是否为标点符号,并在输出结果时不包含标点符号。

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

 1 #include <string>
 2 #include <iostream>
 3 #include <cctype>
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::string;
 8 using std::cerr;
 9 int main()
10 {
11      string s;
12      string result = "";
13      cout<<"Please input a string which must have punctuations."<<endl;
14      cin>>s;
15      int punct_cnt = 0;
16      for(string::size_type index = 0; index < s.size(); ++index){
17         if(ispunct(s[index])){
18             ++punct_cnt;
19         }else {
20             result += s[index];
21         }
22     }
23     if(punct_cnt > 0){
24         cout << "The result of " + s + " without punctions is " + result<<endl;
25     }else {
26         cerr << "Wrong input.The string did not has punctions."<<endl;
27     }
28      system("pause");
29      return 0;
30 }

转载于:https://www.cnblogs.com/zhaokaikai/archive/2012/09/29/2708671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值