用C++编写带有函数的程序,函数表达式fun(str,c)功能是从字符数组str中删除值为c变量的字符。字符中保存字符串,同一字符的大小写按不同字符处理)
时间: 2023-07-15 14:10:42 浏览: 45
下面是一个示例程序,其中包含函数表达式fun(str,c):
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun(char *str, char c) {
int len = strlen(str);
int i, j;
for (i = 0, j = 0; i < len; i++) {
if (tolower(str[i]) != tolower(c)) {
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main() {
char str[100];
char c;
cout << "Please enter a string: ";
cin >> str;
cout << "Please enter a character to remove: ";
cin >> c;
fun(str, c);
cout << "Result: " << str << endl;
return 0;
}
```
该程序通过遍历输入的字符串,跳过与输入字符c相同的字符,将其余字符复制到新的字符串中。由于字符大小写不同被视为不同字符,因此使用 `tolower()` 函数将所有字符转换为小写字符进行比较。最后,将新字符串的末尾设置为零以表示字符串的结束。