Remove punctuation from a String object

Source: Internet
Author: User

Edit a program to remove the punctuation marks from the string object. The string required to be entered into the program must contain punctuation, and the output is the string object after the punctuation is removed.

Eliminate punctuation

#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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.