以c语言的格式写一串代码要求:删除字符串中与某字符相同的字符
时间: 2024-03-09 15:48:21 浏览: 59
好的,以下是 C 语言的代码实现:
```c
#include <stdio.h>
#include <string.h>
char* remove_char(char* input_str, char char_to_remove) {
int len = strlen(input_str);
int j = 0;
for (int i = 0; i < len; i++) {
if (input_str[i] != char_to_remove) {
input_str[j++] = input_str[i];
}
}
input_str[j] = '\0';
return input_str;
}
int main() {
char input_str[100];
char char_to_remove;
printf("Please input a string: ");
scanf("%s", input_str);
printf("Please input a character to remove: ");
scanf(" %c", &char_to_remove);
remove_char(input_str, char_to_remove);
printf("The output string is: %s\n", input_str);
return 0;
}
```
其中,`remove_char` 函数接受两个参数:`input_str` 表示输入的字符串,`char_to_remove` 表示要删除的字符。函数内部使用了一个循环遍历字符串中的每个字符,如果该字符不等于要删除的字符,则将该字符加入到字符串的新位置中。最后将新位置后面的字符全部置为'\0',即字符串的结束标志。函数返回输入字符串。
`main` 函数中首先获取输入字符串和要删除的字符,然后调用 `remove_char` 函数进行处理,最后输出结果。
使用示例:
```
Please input a string: hello world
Please input a character to remove: l
The output string is: heo word
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)