用c语言编写函数,功能为将字符符串中的字符c1用字符c2替换,并加以调用
时间: 2023-06-12 10:04:49 浏览: 104
以下是用 C 语言编写的函数,实现将字符串中的字符 c1 替换为字符 c2 的功能:
```c
#include <stdio.h>
#include <string.h>
void replace_char(char* str, char c1, char c2) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == c1) {
str[i] = c2;
}
}
}
int main() {
char str[100];
char c1, c2;
printf("请输入字符串:");
scanf("%s", str);
printf("请输入要替换的字符:");
scanf(" %c", &c1);
printf("请输入替换成的字符:");
scanf(" %c", &c2);
replace_char(str, c1, c2);
printf("替换后的字符串为:%s\n", str);
return 0;
}
```
在 main 函数中,首先输入原始字符串、要替换的字符 c1 和替换成的字符 c2。然后调用 replace_char 函数,将字符串中的 c1 替换成 c2。最后输出替换后的字符串。
相关问题
C语言编写函数实现查找在两个字符串中同时出现的字符,按公共字符出现的次数进行排 序,返回排序后的结果。主程序中,调用函数实现输入、按从多到少的顺序输出公共字符。
以下是一个可能的 C 语言实现,其中使用了一个简单的结构体存储每个公共字符的信息:
```c
#include <stdio.h>
#include <string.h>
#define MAX_CHARS 256
struct char_info {
char c;
int count;
};
int compare_chars(const void* a, const void* b) {
const struct char_info* c1 = (const struct char_info*) a;
const struct char_info* c2 = (const struct char_info*) b;
return c2->count - c1->count;
}
void find_common_chars(const char* s1, const char* s2, struct char_info* result, int* num_common) {
int count1[MAX_CHARS] = {0};
int count2[MAX_CHARS] = {0};
int i, j;
*num_common = 0;
for (i = 0; s1[i] != '\0'; i++) {
count1[(int) s1[i]]++;
}
for (i = 0; s2[i] != '\0'; i++) {
count2[(int) s2[i]]++;
}
for (i = 0; i < MAX_CHARS; i++) {
if (count1[i] > 0 && count2[i] > 0) {
result[*num_common].c = (char) i;
result[*num_common].count = count1[i] < count2[i] ? count1[i] : count2[i];
(*num_common)++;
}
}
qsort(result, *num_common, sizeof(struct char_info), compare_chars);
}
int main() {
char s1[100], s2[100];
struct char_info common_chars[MAX_CHARS];
int num_common, i;
printf("Enter the first string: ");
scanf("%s", s1);
printf("Enter the second string: ");
scanf("%s", s2);
find_common_chars(s1, s2, common_chars, &num_common);
printf("Common characters and their counts (sorted by count):\n");
for (i = 0; i < num_common; i++) {
printf("%c: %d\n", common_chars[i].c, common_chars[i].count);
}
return 0;
}
```
该程序中,`find_common_chars` 函数接受两个字符串以及一个结构体数组和一个整数指针作为参数,将结果存储在结构体数组中,并将公共字符的数量存储在整数指针中。该函数首先使用两个计数器数组 `count1` 和 `count2` 统计两个字符串中每个字符出现的次数,然后遍历这两个计数器数组,将出现在两个字符串中的字符及其出现的次数存储在结构体数组中。最后,使用 `qsort` 函数对结构体数组按照出现次数进行排序。
在 `main` 函数中,程序首先接受用户输入的两个字符串,然后调用 `find_common_chars` 函数查找这两个字符串中同时出现的字符,并按照出现次数从多到少的顺序输出结果。
C语言用getchar函数读人两个字符给c1和c2,然后分别用putchar 函数和 printf函数输出这两个字符
在C语言中,你可以使用`getchar()`函数一次读入一个字符,如果你想读入两个字符并分别存储在变量`c1`和`c2`中,你需要连续调用两次`getchar()`,因为每次只能读取一个字符。这里是一个示例:
```c
#include <stdio.h>
int main() {
char c1, c2; // 定义两个字符变量
// 读取第一个字符
getchar(); // 这里不会显示输入,因为我们只接收字符
c1 = getchar(); // 现在c1存储了用户输入的第一个字符
// 读取第二个字符
getchar();
c2 = getchar(); // 现在c2存储了用户输入的第二个字符
// 使用putchar()打印出两个字符
putchar(c1);
putchar(c2);
// 或者使用printf()格式化打印,添加换行符\n
printf("字符c1: %c, 字符c2: %c\n", c1, c2);
return 0;
}
```
在这个程序中,先用两次`getchar()`分别将字符存储到`c1`和`c2`,然后通过`putchar()`逐个输出,或者用`printf()`一次性输出并且加上换行符。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)