在字符串中删除与某字符相同的字符,要求用字符数组作函数参数。 程序运行结果示例: Input a string: hello, my friend! Input a character: e Results:hllo, my frind! 输入格式: 输入字符串的提示信息: "Input a string:\n" 输入单个字符的提示信息: "Input a character:\n" 输入格式: 字符串输入用 gets()函数 单个字符输入用 getchar()函数 输出格式: "Results:%s\n" 输入样例: 在这里给出一组输入。例如: hello, my friend! e 输出样例: 在这里给出相应的输出。例如: Input a string: Input a character: Results:hllo, my frind! 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB C (gcc) Selection deleted 1
时间: 2024-03-13 08:44:03 浏览: 45
请看下面的代码实现:
```c
#include <stdio.h>
#include <string.h>
void deleteChar(char str[], char ch) {
int len = strlen(str);
int j = 0;
for (int i = 0; i < len; i++) {
if (str[i] != ch) {
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main() {
char str[1000], ch;
printf("Input a string:\n");
gets(str);
printf("Input a character:\n");
ch = getchar();
deleteChar(str, ch);
printf("Results:%s\n", str);
return 0;
}
```
其中,`deleteChar` 函数用来删除字符串中与指定字符相同的字符,`main` 函数用来输入字符串和指定字符,并输出结果。注意,输入字符串时要用 `gets` 函数,因为 `gets` 函数可以读取一行字符串,包括空格和其他特殊字符;而输入单个字符时要用 `getchar` 函数。另外,在删除字符后要在字符串结尾处添加一个空字符,以表示字符串的结束。
相关问题
在字符串中删除与某字符相同的字符,要求用字符数组作函数参数。 程序运行结果示例: Input a string: hello, my friend! Input a character: e Results:hllo, my frind! 输入格式: 输入字符串的提示信息: "Input a string:\n" 输入单个字符的提示信息: "Input a character:\n" 输入格式: 字符串输入用 gets()函数 单个字符输入用 getchar()函数
好的,下面是一个简单的 C 语言程序,用于实现在字符串中删除与某字符相同的字符:
```c
#include <stdio.h>
#include <string.h>
void remove_char(char str[], char c)
{
int i, j;
int len = strlen(str);
for (i = 0, j = 0; i < len; i++) {
if (str[i] != c) {
str[j++] = str[i];
}
}
str[j] = '\0';
printf("删除后的字符串为:%s\n", str);
}
int main()
{
char str[100], c;
printf("Input a string:\n");
gets(str);
printf("Input a character:\n");
c = getchar();
getchar(); // 读取多余的回车符
remove_char(str, c);
return 0;
}
```
在该程序中,我们首先定义了一个 remove_char 函数,用于实现删除字符串中与某个字符相同的字符。在函数中,我们先获取字符串的长度 len。然后,我们使用一个 for 循环遍历字符串中的每个字符,如果当前字符不等于要删除的字符,将其复制到新的位置 j,同时 j 自增;否则,跳过该字符。最后,我们将字符串的末尾设置为 '\0',表示字符串的结束,并将删除后的字符串输出到屏幕上。
在 main 函数中,我们首先定义一个 char 数组 str 和一个字符 c,分别用于存储用户输入的字符串和要删除的字符。然后,使用 gets 函数从键盘读取用户输入的字符串,使用 getchar 函数读取用户输入的字符,注意需要读取多余的回车符。最后,调用 remove_char 函数进行删除操作。
希望这个程序可以帮助你删除字符串中与某个字符相同的字符。
用c语言在字符串中删除与某字符相同的字符,要求用字符数组作函数参数。 程序运行结果示例: Input a string: hello, my friend! Input a character: e Results:hllo, my frind! 输入格式: 输入字符串的提示信息: "Input a string:\n" 输入单个字符的提示信息: "Input a character:\n" 输入格式: 字符串输入用 gets()函数 单个字符输入用 getchar()函数 输出格式: "Results:%s\n"
以下是用 C 语言实现删除字符串中某个字符的代码:
```c
#include <stdio.h>
#include <string.h>
void delete_char(char str[], char ch)
{
int i, j;
int len = strlen(str);
for (i = j = 0; i < len; i++)
{
if (str[i] != ch)
{
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main()
{
char str[1000];
char ch;
printf("Input a string:\n");
gets(str);
printf("Input a character:\n");
ch = getchar();
delete_char(str, ch);
printf("Results:%s\n", str);
return 0;
}
```
代码中的 `delete_char()` 函数接受一个字符串和一个字符作为参数,然后使用双指针法遍历字符串,将不等于给定字符的字符复制到原字符串中,并在最后添加字符串结束符 `\0`。
在 `main()` 函数中,我们先输入字符串和字符,然后调用 `delete_char()` 函数,并输出结果。请注意,我们使用 `gets()` 函数输入字符串,因为它可以读取包含空格的完整字符串。而单个字符输入使用 `getchar()` 函数。
运行结果:
```
Input a string:
hello, my friend!
Input a character:
e
Results:hllo, my frind!
```