用c语言编程,连接两个字符串并将得到的新字符串中每一个字符升序排列
时间: 2023-11-22 22:55:53 浏览: 89
可以使用C语言中的字符串函数来连接两个字符串,并使用排序算法对新字符串进行升序排列。以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
void sortString(char *str);
int main() {
char str1[50], str2[50], newStr[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
// concatenate two strings
strcpy(newStr, str1);
strcat(newStr, str2);
// sort the new string
sortString(newStr);
printf("New string after sorting: %s\n", newStr);
return 0;
}
void sortString(char *str) {
int i, j, len;
char temp;
len = strlen(str);
for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
if (str[i] > str[j]) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
```
在上面的代码中,我们首先使用 `strcpy()` 和 `strcat()` 函数来连接两个字符串。然后,我们使用 `sortString()` 函数对新字符串进行升序排列。该函数使用冒泡排序算法,将字符串中的字符按照 ASCII 码从小到大排序。最后,我们输出排序后的新字符串。
阅读全文
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.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)
![](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)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)