问题描述
编写一个程序,先输入一个字符串str(长度不超过20),再输入单独的一个字符ch,然后程序会把字符串str当中出现的所有的ch字符都删掉,从而得到一个新的字符串str2,然后把这个字符串打印出来。
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[25];
fgets(s,25,stdin);
char c;
cin >> c;
int len = strlen(s);
for (int i = 0; i < len; ) {
if (s[i] == c) {
for (int j = i + 1; j < len; j++) {
s[j - 1] = s[j];
}
len--;
}
else i++;
}
cout << s;
return 0;
}