编一个程序,从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;
}
|