程序应该用“!”替换任何标点符号。问题是任何字符都被替换了。
//Замена в строке знаков препинания на "!"
#include <iostream>
#include <Windows.h>
using namespace std;
void replace(char* str) {
int quantity = strlen(str);
for (int i = 0; i < quantity; i++) {
if (str[i] == ',' , '?' , '.' , ':' , ';') {
str[i] = '!';
}
cout << str[i];
}
cout << endl;
}
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
char str[1000];
cout << "Введите строку: ";
cin.getline(str, 1000);
replace(str);
system("pause");
return 0;
}
奇迹出现了:
if (str[i] == ',' , '?' , '.' , ':' , ';')
替换为一系列结合逻辑 OR 的比较