社区
C++ 语言
帖子详情
从string对象中去除标点符号.
「已注销」
2007-01-25 10:36:29
有一道题:
编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号的string对象.如何实现,有简单易懂的程序实现。
...全文
1049
19
打赏
收藏
从string对象中去除标点符号.
有一道题: 编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号的string对象.如何实现,有简单易懂的程序实现。
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
19 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
dr_lou
2007-01-27
打赏
举报
回复
public class myString
{
public static void main(String [] args)
{
String s = "My,Name.Is!Xu!";
char [] chs = s.toCharArray();
for(int i=0;i<chs.length;i++)
{
if(Character.isDigit(chs[i])==false && Character.isLetter(chs[i])==false)
{
chs[i]='\0';
}
}
String newString = String.valueOf(chs);
System.out.println(newString);
}
}
cutftp
2007-01-26
打赏
举报
回复
不会,只会CString
OOPhaisky
2007-01-26
打赏
举报
回复
虫子和taodm的代码不错啊
HewpKanXue
2007-01-26
打赏
举报
回复
顶一下用谓词的
taodm
2007-01-26
打赏
举报
回复
bool NotNeed(char c)
{
return ispunct(c) || isspace(c);
}
int main()
{
string s("my name is zhao,and i'm ten years old.");
s.erase(remove_if(s.begin(), s.end(), NotNeed), s.end());
cout << s;
system("pause");
return 0;
}
yantao0515
2007-01-26
打赏
举报
回复
一个简单思路
用isalpha和isnumber判断,如若两者都不是,则为符号或着空格
不知道行不?
pitter211
2007-01-26
打赏
举报
回复
fangrk(加把油,伙计!) 和taodm(taodm) 都很经典.增加了见识.区区一个串处理,引出如此多的方法.佩服.受用无穷,顶一个.
pse_sun
2007-01-26
打赏
举报
回复
一個思路:
string str = "my name is zhao,and i'm ten years old.";
string strDes = "";
for (int n = 0; n < str.length(); n++)
{
char cTmp = str.at(n);
if ((cTmp > 47 && cTmp < 58) || (cTmp > 64 && cTmp < 91) || (cTmp > 96 && cTmp < 123))
{
strDes += cTmp;
}
}
cout << strDes;
mr_moran
2007-01-26
打赏
举报
回复
该说的别人都说了,顶一个。
「已注销」
2007-01-26
打赏
举报
回复
刚我编的那一段代码会重复标点后的那个字母,如何解决?
「已注销」
2007-01-26
打赏
举报
回复
如果是一句话的话,如何把其中的空格去掉。只保留字母和数学。如何处理。
比如果是这样一句话.my name is zhao,and i'm ten years old.处理结果是:mynameiszhaoandimtehnyearsodl
「已注销」
2007-01-26
打赏
举报
回复
其实这样就可以实际:
#include<string>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
cin>>s1;
string::size_type index;
for(index=0;index<s1.size();index++)
{
if(ispunct(s1[index]))
{
s1[index]=s1[index+1];
index=index+1;
}
}
cout<<s1;
}
我想问的是,如果是一名话,中间有空格的话如何处理呢?
jixingzhong
2007-01-26
打赏
举报
回复
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Please enter a string:";
cin >> str;
cout << endl;
string filter("\",.;:?!\'");
string::size_type pos = 0;
while ((pos = str.find_first_of(filter, pos)) != string::npos)
{
str.erase(pos, 1);
}
cout << str << endl;
return 0;
}
taodm
2007-01-26
打赏
举报
回复
用remove_if加erase,看effective stl item32
DragonBill
2007-01-26
打赏
举报
回复
用string::find_first_of
fangrk
2007-01-26
打赏
举报
回复
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;
struct Filter:public unary_function<char,bool>
{
public:
Filter(const string& f):filter(f){}
bool operator()(char ch) const {return filter.find(ch)!=string::npos;}
private:
string filter;
};
int main()
{
Filter F("\",.;:?!)(\\/'");
string inputString;
getline(cin,inputString);
inputString.erase(remove_if(inputString.begin(),inputString.end(),F),inputString.end());
cout<<inputString;
return 0;
}
G:\test>a
Hello,This a demo of csdn's question!
HelloThis a demo of csdns question
G:\test>
todototry
2007-01-25
打赏
举报
回复
int main()
{
string str;
cout << "Please enter a string:";
cin >> str;
cout << endl;
string caps(",.\'\"");
string::size_type pos = 0;
while ((pos = str.find_first_of(caps, pos)) != string::npos)
{
str.erase(pos, 1);
}
cout << str << endl;
return 0;
}
happyzqj
2007-01-25
打赏
举报
回复
#include <iostream>
#include <string>
using namespace std;
int main()
{
string filter("\",.;:?!)(\\/");
string inputString;
cin>>inputString;
string::size_type pos=0;
while((pos = inputString.find_first_of(filter,pos)) != string::npos)
{
inputString.erase(pos,1);
}
cout<<inputString;
return 0;
}
jeelypine
2007-01-25
打赏
举报
回复
先看题目了,呵呵
java
去除
空格、
标点符号
的方法实例.docx
Java语言
中
去除
空格、
标点符号
是一种常见的字符串处理操作,本文将通过实例代码详细介绍如何使用Java语言
去除
空格、
标点符号
。
去除
空格的方法 在Java
中
,
去除
空格可以使用trim()、replace()、replaceAll()等方法...
python3去掉
string
中
的
标点符号
方法
网上看到的python去掉字符串
中
的
标点符号
的方法,大多是基于python2的,不适用python3,调整后代码如下: 代码 lower_case_documents = ['Hello, how are you!','Win money, win from home.','Call me now.','Hello...
Python处理
中
文
标点符号
大集合
如果不希望使用`zhon`包,也可以通过Python标准库
中
的`
string
.punctuation`获取英文
标点符号
集合。 在
去除
标点时,可以使用`re.sub`函数配合正则表达式来实现。如果需要
去除
所有
标点符号
,可以构造一个包含所有
中
文...
java
去除
空格、
标点符号
的方法实例
在文本处理
中
,
去除
字符串
中
的空格和
标点符号
是常见的需求。本文将详细介绍Java
中
去除
空格和
标点符号
的方法,通过具体的示例代码加深理解。 1.
去除
空格 在Java
中
,
去除
字符串两端的空格,可以使用
String
类的trim...
C++软件技术习题与答案(1)
### C++程序设计知识点解析:从
string
对象
中
去掉
标点符号
#### 题目概述 本题目要求设计并实现一个C++程序,该程序能够读取用户输入的一段文本,并
去除
其
中
的所有
标点符号
。如果输入的文本
中
含有
标点符号
,则输出...
C++ 语言
65,186
社区成员
250,526
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章