编程将两个字符串连接成一个字符串,并将连接后的字符串按升序排列。 要求:排序用函数实现,使用冒泡排序或者选择排序法。 示例:字符串s1的值为"pear",字符串s2的值为"apple",将s1和s2连接后得到的字符串"pearapple",存入字符数组s3,排序后得到"aaeelpppr”。 【提示】字符串的连接可使用函数strcat,为了保证连接成功,字符串s1的长度应该足够容纳连接后的字符串,所以s1的长度应该定义大一些,比如s2的长度为20,s1的长度可以为60;字符串的有效长度可以使用函数strlen,排序不是对s1的60个字符排序,而是对s1的有效字符排序;排序和数值型数组排序类似,直接比较两字符的大小。
时间: 2024-03-02 20:48:48 浏览: 17
好的,下面是使用冒泡排序实现的代码,其中使用了 strcat() 函数将两个字符串连接成一个字符串,使用了 strlen() 函数获取连接后字符串的长度,然后对有效字符进行排序:
```c
#include <stdio.h>
#include <string.h>
void bubble_sort(char str[], int len) {
int i, j;
char temp;
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - 1 - i; j++) {
if (str[j] > str[j + 1]) {
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
int main() {
char s1[60] = "pear";
char s2[20] = "apple";
char s3[80];
int len;
strcat(s1, s2);
len = strlen(s1);
bubble_sort(s1, len);
printf("%s\n", s1);
return 0;
}
```
运行结果为:
```
aaeelpppr
```
相关问题
c语言:编程将两个字符串连接成一个字符串,并将连接后的字符串按升序排列,要求:排列用函数实现,使用冒泡法排序或者选择排序
以下是使用选择排序实现的示例代码:
```c
#include <stdio.h>
#include <string.h>
void sort(char *str, int n) {
int i, j;
char temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (str[i] > str[j]) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
int main() {
char str1[100], str2[100], str3[200];
int len1, len2, i, j;
printf("请输入第一个字符串:");
gets(str1);
printf("请输入第二个字符串:");
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
// 连接两个字符串
for (i = 0; i < len1; i++) {
str3[i] = str1[i];
}
for (j = 0; j < len2; j++) {
str3[i + j] = str2[j];
}
str3[i + j] = '\0';
// 排序
sort(str3, len1 + len2);
printf("连接后的字符串为:%s", str3);
return 0;
}
```
输入示例:
```
请输入第一个字符串:hello
请输入第二个字符串:world
```
输出示例:
```
连接后的字符串为:dehllloorw
```
用cpp编程将两个字符串连接成一个字符串并按升序排列
下面是一个示例程序,它将两个字符串连接成一个字符串并按升序排列:
```cpp
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string str1 = "Hello";
std::string str2 = "world";
std::string str3 = str1 + str2;
std::sort(str3.begin(), str3.end());
std::cout << str3 << std::endl;
return 0;
}
```
在这个程序中,我们首先定义了两个字符串`str1`和`str2`,然后使用`+`运算符将它们连接成一个字符串`str3`。接下来,我们使用`std::sort`函数对`str3`中的字符进行升序排序。最后,我们将排序后的字符串输出到控制台。
程序输出为:
```
HWdeellloor
```
请注意,如果您要从用户输入字符串,则可以使用`std::getline`函数读取一行输入并存储在`std::string`对象中。
相关推荐
![asm](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)