如何消除标点符号
来源:优易学  2011-12-31 15:01:08   【优易学:中国教育考试门户网】   资料下载   IT书店
  编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号后的string 对象。
  消除标点
  #include <iostream>
  #include <string>
  #include <cctype>
  using namespace std;
  int main()
  {
  string s, result_str;
  bool has_punct = false;  //用于标记字符串中有无标点
  char ch;      //输入字符串
  cout << "Enter a string:" << endl;
  getline(cin, s);    //处理字符串:去掉其中的标点
  for (string::size_type index = 0; index != s.size(); ++index)
  {
  ch = s[index];
  if (ispunct(ch))
  has_punct = true;
  else
  result_str += ch;
  }
  if (has_punct)
  cout << "Result:" << endl << result_str <<endl;
  else
  {
  cout << "No punctuation character in the string?!" << endl;
  return -1;
  }
  return 0;
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训