c语言给定程序的功能是,将仅在字符串 s 中出现而不在字符串 t 中出现的字符,和仅在 t 中出现而不在字符串 s 中出现的字符,构成一个新的字符串放在 u 中,u 中字符按原字符串 中字符的顺序排列,不去掉重复字符。
时间: 2024-05-01 10:19:45 浏览: 70
下面是一个实现上述功能的C语言程序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
void find_unique_chars(char s[], char t[], char u[]) {
int len_s = strlen(s);
int len_t = strlen(t);
int len_u = 0;
// 遍历字符串s
for (int i = 0; i < len_s; i++) {
int found = 0;
// 检查字符是否在字符串t中出现
for (int j = 0; j < len_t; j++) {
if (s[i] == t[j]) {
found = 1;
break;
}
}
// 如果字符仅在字符串s中出现,则添加到字符串u中
if (!found) {
u[len_u++] = s[i];
}
}
// 遍历字符串t
for (int i = 0; i < len_t; i++) {
int found = 0;
// 检查字符是否在字符串s中出现
for (int j = 0; j < len_s; j++) {
if (t[i] == s[j]) {
found = 1;
break;
}
}
// 如果字符仅在字符串t中出现,则添加到字符串u中
if (!found) {
u[len_u++] = t[i];
}
}
// 在字符串u的末尾添加空字符
u[len_u] = '\0';
}
int main() {
char s[MAX_LEN], t[MAX_LEN], u[MAX_LEN];
printf("请输入字符串s:");
scanf("%s", s);
printf("请输入字符串t:");
scanf("%s", t);
find_unique_chars(s, t, u);
printf("仅在字符串s中出现而不在字符串t中出现的字符和仅在字符串t中出现而不在字符串s中出现的字符为:%s\n", u);
return 0;
}
```
示例输入输出:
```
请输入字符串s:hello
请输入字符串t:world
仅在字符串s中出现而不在字符串t中出现的字符和仅在字符串t中出现而不在字符串s中出现的字符为:heowrdl
```
阅读全文
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)