在C++Primer看到该题,入门小白,希望得到批评。
基本思路:
1.对每个字符进行判断是否为标点符号,使用基于范围的for语句;
2.如何去掉标点符号之后将剩余字符连接起来?使用string的相加操作。
#include "stdafx.h"
#include <string>
#include <iostream>
//包含标准库的对象
using std::cin;
using std::cout;
using std::string;
using std::endl;
int main()
{
string str1,str2;
cin >> str1;
//auto获取str1的类型
for (auto c : str1) {
//将非标点符号的字符加到str2的后边
if (!ispunct(c))
str2 += c;
}
cout << str1 << endl << str2 << endl;
getchar();
return 0;
}