exercises 3.6-3.10

本文提供了几个使用C++进行字符串处理的例子,包括遍历并替换非空格字符、移除句子中的标点符号等,有助于理解如何利用C++标准库进行高效字符串操作。
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 
 6 int main()
 7 {
 8     //3.6
 9     string str("some thing");
10     for(decltype(str.size()) i = 0 ; i<str.size() ; ++i)
11         if(!isspace(str[i]))str[i]='X';
12     
13     cout<<str<<endl;
14     //3.7
15     str="some thing";
16     for(auto &c:str)
17         if(!isspace(c))
18             c='X';
19             
20     cout<<str<<endl;
21     //3.8
22     str="some thing";
23     string::size_type index=0;
24     while(index<str.size()){    
25         if(!isspace(str[index]))str[index]='X';
26         index++;
27     }
28     cout<<str<<endl;
29     //3.10
30     string  sentence,osentence;
31     cin>>sentence;
32     for(auto c:sentence){
33         if(!ispunct(c))osentence+=c;
34     } 
35     cout<<osentence<<endl;
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/austuode/p/4431496.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值