输入三个字符串,按由小到大输出,C语言指针
时间: 2023-08-05 18:15:27 浏览: 63
好的,我来为您解答。
您可以这样实现:
```c
#include <stdio.h>
#include <string.h>
void sort(char* a, char* b, char* c) {
char* tmp;
if (strcmp(a, b) > 0) {
tmp = a;
a = b;
b = tmp;
}
if (strcmp(b, c) > 0) {
tmp = b;
b = c;
c = tmp;
}
if (strcmp(a, b) > 0) {
tmp = a;
a = b;
b = tmp;
}
printf("%s\n%s\n%s\n", a, b, c);
}
int main() {
char str1[100], str2[100], str3[100];
printf("请输入三个字符串:\n");
scanf("%s%s%s", str1, str2, str3);
sort(str1, str2, str3);
return 0;
}
```
这段代码中,`sort` 函数用来对三个字符串进行排序,并按顺序输出。`main` 函数中先读入三个字符串,然后调用 `sort` 函数进行排序。
希望可以帮助到您!
相关问题
c语言用指针输入3个字符串,按由小到大的顺序输出
您好,这是一个C语言实现用指针输入3个字符串,按由小到大的顺序输出的示例代码:
```
#include <stdio.h>
#include <string.h>
void swap(char **a, char **b) {
char *tmp = *a;
*a = *b;
*b = tmp;
}
void sort(char **strs, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(strs[i], strs[j]) > 0) {
swap(&strs[i], &strs[j]);
}
}
}
}
int main() {
char *strs[3];
for (int i = 0; i < 3; i++) {
char str[100];
printf("请输入第%d个字符串:", i + 1);
scanf("%s", str);
strs[i] = strdup(str); // strdup() 可以复制字符串
}
sort(strs, 3);
printf("排序后的字符串依次为:\n");
for (int i = 0; i < 3; i++) {
printf("%s\n", strs[i]);
free(strs[i]); // 释放 strdup() 分配的空间
}
return 0;
}
```
您可以将上面的代码保存到一个文件中(例如文件名为 `sort_strings.c`),然后在命令行中使用以下命令编译运行这个程序:
```
$ gcc -o sort_strings sort_strings.c
$ ./sort_strings
```
然后按照程序提示输入三个字符串即可,程序将按照从小到大的顺序输出这三个字符串。
由键盘输入三个字符串 按由小到大顺序输出 用到指针同时带有一定解释
可以使用C语言的字符串库函数`strcmp()`来比较字符串的大小,然后使用指针数组来存储这三个字符串,并使用循环和条件语句来进行排序。以下是一个示例代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[3][100]; // 定义一个二维数组,存储三个字符串
char temp[100]; // 定义一个临时数组,用于交换字符串
// 从键盘读入三个字符串
for (int i = 0; i < 3; i++) {
printf("请输入第%d个字符串:", i + 1);
scanf("%s", str[i]);
}
// 冒泡排序,按照字符串大小从小到大排序
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2 - i; j++) {
if (strcmp(str[j], str[j + 1]) > 0) {
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
// 输出排序后的字符串
printf("按照字典序从小到大排序后的字符串为:\n");
for (int i = 0; i < 3; i++) {
printf("%s\n", str[i]);
}
return 0;
}
```
在这个代码中,我们定义了一个二维数组`str`,用于存储三个字符串,同时定义了一个临时数组`temp`,用于交换字符串。然后使用循环从键盘中读入三个字符串。接着,使用冒泡排序的方法,从小到大排序这三个字符串。最后,使用循环输出排序后的字符串。需要注意的是,这里的字符串数组是二维数组,因为每个字符串有可能会占用多个字符的存储空间。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_column_c1.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)