Comparison of codeup exercise strings

 

Title description

Enter 3 character strings and output them in ascending order. Requires the use of pointers for processing.

enter

3 lines, one string per line. Ensure that the length of each string does not exceed 20.

Output

Output these 3 character strings in ascending order, with one line per character string.
Please pay attention to the line ending output.

Sample input Copy

China
CLOCK
deal

Sample output Copy

CLOCK
China
deal

AC code:

#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    char str1[20],str2[20],str3[20],temp[20];
    gets(str1);
    gets(str2);
    gets(str3);
    if (strcmp(str1,str2) > 0)
    {
        strcpy(temp,str1);
        strcpy(str1,str2);
        strcpy(str2,temp);
    }
    if (strcmp(str1,str3) > 0)
    {
        strcpy(temp,str3);
        strcpy(str3,str1);
        strcpy(str1,temp);
    }
    if (strcmp(str2,str3) > 0)
    {
        strcpy(temp,str3);
        strcpy(str3,str2);
        strcpy(str2,temp);
    }
    puts(str1);
    puts(str2);
    puts(str3);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/smallrain6/article/details/107130516