Easy Tutorial
❮ C Examples Swapping Cyclic Order C Exercise Example32 ❯

C Exercise Example 79

C Language Classic 100 Examples

Title: String Sorting.

Program Analysis: None.

Program Source Code:

Example

// Created by www.tutorialpro.org on 15/11/9.
// Copyright © 2015 tutorialpro.org. All rights reserved.

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

void swap(char*str1,char*str2);
int main()
{
    char str1[20],str2[20],str3[20];
    printf("Please enter 3 strings, each string ends with a newline!:\n");
    fgets(str1, (sizeof str1 / sizeof str1[0]), stdin);
    fgets(str2, (sizeof str2 / sizeof str2[0]), stdin);
    fgets(str3, (sizeof str3 / sizeof str3[0]), stdin);
    if(strcmp(str1,str2)>0)swap(str1,str2);
    if(strcmp(str2,str3)>0)swap(str2,str3);
    if(strcmp(str1,str2)>0)swap(str1,str2);
    printf("The sorted result is:\n");
    printf("%s\n%s\n%s\n",str1,str2,str3);
    return 0;
}
void swap(char*str1,char*str2)
{
    char tem[20];
    strcpy(tem,str1);
    strcpy(str1,str2);
    strcpy(str2,tem);
}

The above example output results are:

Please enter 3 strings, each string ends with a newline!:
b
a
t
The sorted result is:
a
b
t

C Language Classic 100 Examples

❮ C Examples Swapping Cyclic Order C Exercise Example32 ❯