Exercice_3.10_去掉string对象中的标点符号

//去掉string对象中的标点符号
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str, result_str;
    bool has_punct = false; //用于记录有无标点
    char ch;

    //输入字符串
    cout << "Enter strings." << endl;
    getline(cin, str);

    //去掉字符串中的标点 换个思路也就是保存不是标点的字符
    for (string::size_type index = 0; index != str.size(); ++index)
    {
        ch = str[index];
        if (ispunct(ch))
            has_punct = true;
        else
            result_str += ch;
    }

    //输出结果
    if(has_punct)
        cout << "Result: " << result_str << endl;
    else
    {
        cout << "No punctuations in the strings." << endl;
        return -1;
    }

    return 0;
}

posted @ 2014-05-04 23:00  庄浩  阅读(172)  评论(0编辑  收藏  举报