使用c语言,写一个输入一行字符可以将其中每个字符从小到大排列后输出的程序
时间: 2024-05-15 14:18:41 浏览: 18
以下是使用C语言编写的将输入的一行字符从小到大排列后输出的程序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char str[MAX_LEN];
int len, i, j, temp;
printf("请输入一行字符:");
fgets(str, MAX_LEN, stdin);
len = strlen(str);
// 冒泡排序
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;
}
}
}
printf("排序后的字符为:");
for (i = 0; i < len; i++) {
printf("%c", str[i]);
}
return 0;
}
```
程序中使用了冒泡排序的算法,将输入的字符从小到大排列后输出。其中,fgets() 函数可以读入一行字符,strlen() 函数可以获取字符串的长度。
相关问题
用c语言写程序,输入三个字符串,按从小到大的顺序输出
好的,这个问题很简单,下面是代码实现:
```
#include <stdio.h>
#include <string.h>
void swap(char *str1, char *str2) {
char temp[100];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
int main() {
char str1[100], str2[100], str3[100];
printf("请输入三个字符串:\n");
scanf("%s %s %s", str1, str2, str3);
if (strcmp(str1, str2) > 0) swap(str1, str2);
if (strcmp(str1, str3) > 0) swap(str1, str3);
if (strcmp(str2, str3) > 0) swap(str2, str3);
printf("三个字符串从小到大的顺序是:%s %s %s\n", str1, str2, str3);
return 0;
}
```
运行结果如下:
```
请输入三个字符串:
abc
def
ghi
三个字符串从小到大的顺序是:abc def ghi
```
写一个C语言使用起泡法和选择法将字符串按ASCⅡ码从小到大排列,并举例
好的,这是一个比较基础的排序问题,下面是使用起泡法和选择法将字符串按ASCⅡ码从小到大排列的C语言代码:
使用起泡法:
```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 str[] = "chitgpt";
int len = strlen(str);
bubble_sort(str, len);
printf("排序后的字符串为:%s\n", str);
return 0;
}
```
使用选择法:
```c
#include <stdio.h>
#include <string.h>
void selection_sort(char *str, int len) {
int i, j, min_index;
char temp;
for (i = 0; i < len - 1; i++) {
min_index = i;
for (j = i + 1; j < len; j++) {
if (str[j] < str[min_index]) {
min_index = j;
}
}
if (min_index != i) {
temp = str[i];
str[i] = str[min_index];
str[min_index] = temp;
}
}
}
int main() {
char str[] = "chitgpt";
int len = strlen(str);
selection_sort(str, len);
printf("排序后的字符串为:%s\n", str);
return 0;
}
```
以上代码都是将字符串 "chitgpt" 按ASCⅡ码从小到大排列。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)