社区
C++ 语言
帖子详情
从string对象中去除标点符号.
「已注销」
2007-01-25 10:36:29
有一道题:
编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号的string对象.如何实现,有简单易懂的程序实现。
...全文
994
19
打赏
收藏
从string对象中去除标点符号.
有一道题: 编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号的string对象.如何实现,有简单易懂的程序实现。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
先看题目了,呵呵
C++软件技术习题与答案(1)
编写一个程序,从
string
对象
中
去掉
标点符号
,要求如果输入的字符串有
标点符号
,输出结果则是去掉
标点符号
后的
string
对象
;如果输入的字符串没有
标点符号
,则原样输出
python3去掉
string
中
的
标点符号
方法
网上看到的python去掉字符串
中
的
标点符号
的方法,大多是基于python2的,不适用python3,调整后代码如下: 代码 lower_case_documents = ['Hello, how are you!','Win money, win from home.','Call me now.','Hello...
JS去掉字符串末尾的
标点符号
及删除最后一个字符的方法
需求:去掉js字符串末尾的
标点符号
原字符串: Hello World! 目标字符串: Hello World 方式一:
string
Object.slice(start,end) start : 要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始...
DocumentAnalysis:使用 Hadoop 进行维基百科文档分析
其
中
key没有意义,value就是这两个标签(包括标签本身)的值,可以通过.to
String
()方法转化为字符串进行下一步处理src/documentParserTextParser.java正则表达式处理
String
,能够
去除
大部分
标点符号
,需要补全...
string
对象
中
去掉
标点符号
题目要求:编写一个程序,从
string
对象
中
去掉
标点符号
。要求输入到程序的字符串必须必须包含
标点符号
,输出结果则是去掉
标点符号
后的
string
对象
。 int main() {
string
str;
string
new_str; int pos=0,new_pos=0;...
C++ 语言
64,651
社区成员
250,488
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章