编写带有函数的程序,函数表达式fun(str,c)功能是从字符数组str中删除值为c变量的字符。str中保存字符串,同一字母的大小写按不同字符处理。
时间: 2023-07-14 09:12:11 浏览: 43
下面是一个示例代码,其中使用了指针和循环来遍历字符串并删除目标字符:
```c
#include <stdio.h>
void fun(char *str, char c) {
int i = 0, j = 0;
while (str[i] != '\0') {
if (str[i] != c && str[i] != c - 32 && str[i] != c + 32) {
str[j] = str[i];
j++;
}
i++;
}
str[j] = '\0';
}
int main() {
char str[] = "Hello World!";
char c = 'o';
printf("Original string: %s\n", str);
fun(str, c);
printf("String after removing '%c': %s\n", c, str);
return 0;
}
```
运行结果如下:
```
Original string: Hello World!
String after removing 'o': Hell Wrld!
```
相关问题
用C++编写带有函数的程序,函数表达式fun(str,c)功能是从字符数组str中删除值为c变量的字符。字符中保存字符串,同一字符的大小写按不同字符处理)
以下是用C++编写带有函数的程序,函数表达式fun(str,c)功能是从字符数组str中删除值为c变量的字符。
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun(char str[], char c)
{
int len = strlen(str);
int index = 0;
for (int i = 0; i < len; i++)
{
if (str[i] != c && str[i] != (c + 32) && str[i] != (c - 32))
{
str[index++] = str[i];
}
}
str[index] = '\0';
}
int main()
{
char str[100];
char c;
cout << "请输入字符串:";
cin.getline(str, 100);
cout << "请输入要删除的字符:";
cin >> c;
fun(str, c);
cout << "删除后的字符串为:" << str << endl;
return 0;
}
```
在上面的代码中,我们首先定义了一个函数`fun`,它有两个参数,分别是字符数组`str`和字符`c`。函数的功能是从字符数组中删除所有值为`c`的字符。字符中保存字符串,同一字符的大小写按不同字符处理。
在函数中,我们首先获取字符数组的长度`len`,然后用一个循环遍历整个字符数组。在循环中,如果当前字符不是要删除的字符以及其大小写形式,我们就将其存入新的字符数组中,同时更新存入字符数组的下标`index`。
最后,我们在新的字符数组末尾加上一个空字符`\0`,以表示字符串的结尾。在主函数中,我们通过`cin.getline()`函数获取输入的字符串,然后调用`fun()`函数进行字符的删除操作。最后,输出删除后的字符串。
编写带有函数的程序,函数表达式fun(str,c)功能是从字符数组str中删除值为c变量的字符。字符中保存字符串,同一字符的大小写按不同字符处理)
以下是一个实现了函数表达式fun(str,c)的示例程序:
```python
def fun(str, c):
# 初始化一个空字符串用于存储结果
result = ""
# 遍历字符数组中的每个字符
for i in str:
# 如果不是要删除的字符,则将其添加到结果字符串中
if i != c:
result += i
# 返回结果字符串
return result
# 示例用法
str = "Hello, World!"
c = "o"
result = fun(str, c)
print(result) # 输出结果为 "Hell, Wrld!"
```
在上面的示例程序中,我们定义了一个名为`fun`的函数,它接受两个参数`str`和`c`。`str`参数是一个字符数组,`c`参数是我们要删除的字符。在函数内部,我们首先初始化一个空字符串`result`用于存储结果。然后,我们遍历字符数组中的每个字符。如果该字符不是要删除的字符`c`,则将其添加到结果字符串中。最后,我们返回结果字符串。
在示例用法中,我们将一个字符串和要删除的字符传递给函数`fun`,并将返回的结果字符串打印出来。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)