鱼C论坛

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

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

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

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

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

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

RT,萌新的想法貌似行不通,有大神有其他想法吗
#include<iostream>
#include<string>
using namespace std;
int main(){
        string a("abc,.d'e");
        
        for(decltype(a.size())b=0;b<a.size();++b){
                if(ispunct(a[b])){
                        a[b]='';}
        }
        cout<<a<<endl;
return 0;
}
最佳答案
2018-10-9 13:41:30
#include <iostream>
#include <string>
#include <cctype>

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

        for(auto iter = string.begin(); iter != string.end(); ++iter)
        {
                if(ispunct(*iter))
                {
                        iter = string.erase(iter);
                        --iter;
                }
        }

        std::cout << string << std::endl;
        return 0;
}
2.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-9 12:00:28 | 显示全部楼层
引号里面要空格
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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


                               
登录/注册后可看大图

这个中间多出空格了

                               
登录/注册后可看大图

得要这样子的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

        for(auto iter = string.begin(); iter != string.end(); ++iter)
        {
                if(ispunct(*iter))
                {
                        iter = string.erase(iter);
                        --iter;
                }
        }

        std::cout << string << std::endl;
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-2 16:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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