鱼C论坛

 找回密码
 立即注册
查看: 2011|回复: 3

[已解决]c++如何将标点符号去除后输出字符串剩余的部分

[复制链接]
发表于 2018-10-9 11:37:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 469826336 于 2018-10-9 11:39 编辑

RT,萌新的想法貌似行不通,有大神有其他想法吗
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main(){
  5.         string a("abc,.d'e");
  6.        
  7.         for(decltype(a.size())b=0;b<a.size();++b){
  8.                 if(ispunct(a[b])){
  9.                         a[b]='';}
  10.         }
  11.         cout<<a<<endl;
  12. return 0;
  13. }
复制代码

最佳答案
2018-10-9 13:41:30
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>

  4. int main()
  5. {
  6.         std::string string("abc,.d'e");

  7.         for(auto iter = string.begin(); iter != string.end(); ++iter)
  8.         {
  9.                 if(ispunct(*iter))
  10.                 {
  11.                         iter = string.erase(iter);
  12.                         --iter;
  13.                 }
  14.         }

  15.         std::cout << string << std::endl;
  16.         return 0;
  17. }
复制代码
2.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-9 12:00:28 | 显示全部楼层
引号里面要空格
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-9 13:10:28 | 显示全部楼层
claws0n 发表于 2018-10-9 12:00
引号里面要空格


                               
登录/注册后可看大图

这个中间多出空格了

                               
登录/注册后可看大图

得要这样子的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-9 13:41:30 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>

  4. int main()
  5. {
  6.         std::string string("abc,.d'e");

  7.         for(auto iter = string.begin(); iter != string.end(); ++iter)
  8.         {
  9.                 if(ispunct(*iter))
  10.                 {
  11.                         iter = string.erase(iter);
  12.                         --iter;
  13.                 }
  14.         }

  15.         std::cout << string << std::endl;
  16.         return 0;
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 10:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表