c语言编写程序编写一个函数void str_binchar str1 char str2 str1、str2是两个有序字符串其中字符按ASCII码从小到大排序将str2合并到字符串str1中要求合并后的字符串仍是有序的允许字符重复。在main函数中测试该函数:从键盘输入两个有序字符串然后调用该函数最后输出合并后的结果。
#include <stdio.h> #include <string.h>
void str_bin(char str1[], char str2[]) { int len1 = strlen(str1); int len2 = strlen(str2); int i = len1 - 1; int j = len2 - 1; int k = len1 + len2 - 1;
while (i >= 0 && j >= 0) {
if (str1[i] > str2[j]) {
str1[k--] = str1[i--];
} else {
str1[k--] = str2[j--];
}
}
while (j >= 0) {
str1[k--] = str2[j--];
}
}
int main() { char str1[100], str2[100]; printf("Enter the first string: "); scanf("%s", str1); printf("Enter the second string: "); scanf("%s", str2); str_bin(str1, str2); printf("Merged string: %s\n", str1); return 0;
原文地址: https://www.cveoy.top/t/topic/guQR 著作权归作者所有。请勿转载和采集!