以下代码可以正确地从字符数组中去除标点符号:
#include <cctype>
#include <iostream>
int main()
{
char line[] = "ts='TOK_STORE_ID'; one,one, two;four$three two";
for (char* c = line; *c; c++)
{
if (std::ispunct(*c))
{
*c = ' ';
}
}
std::cout << line << std::endl;
}
如果line
是std::string
类型,那么这段代码会是什么样子?
std::string
的数组访问运算符[]
来操作字符串中的单个字符。 - πάντα ῥεῖ