![]() |
#2
lintaoyn2010-05-10 19:19
|
大家好:请教各位高手个问题(红色标注)
编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含
有标点符号,输出结果则是去掉标点符号后的string 对象。
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; 这句的作用是什么?既然ispunct检测到标点符号返回ture就该接着处理,那为什么result_str += ch;要放在else后?
这样一来是不是将处理标点符号的作用给跳过去了呢???
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;
}
谢谢大家此看帖