要求:输入一个字符串,将其中的标点符号去除后输出。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string input;
string output;
// Read a string from cin
cout << "Input string: ";
getline(cin, input);
// Process this string
for (string::size_type i = 0; i < input.size(); i++)
{
if (!ispunct(input[i]))
{
output += input[i];
}
}
// Output new string
cout << "Output string: " << output << endl;
return 0;
}[chapter3]$ ./a.out
Input string: a!b@c#d$e%f^g&
Output string: abcdefg

本文介绍了一个简单的C++程序,该程序能够读取用户输入的字符串,并移除其中的所有标点符号。通过使用标准库函数`ispunct()`来判断字符是否为标点符号,并仅保留非标点符号字符。

4604

被折叠的 条评论
为什么被折叠?



