从string对象中去除标点符号.

「已注销」 2007-01-25 10:36:29
有一道题:
编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号的string对象.如何实现,有简单易懂的程序实现。
...全文
994 19 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
先看题目了,呵呵

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧