current position:Home>PTA 7-1 string sorting (10 points)

PTA 7-1 string sorting (10 points)

2022-02-02 11:39:55 CSDN Q & A

This question requires the preparation of procedures , Read in 5 A string , Output in order of small to large .
Input format :
Input is space delimited 5 Non empty strings , Each string does not contain spaces 、 tabs 、 Blank characters such as line breaks , The length is less than 80.
Output format :
Output the sorted results in the following format :
After sorted:
One string per line
sample input :
red yellow blue black white
No blank lines at the end

  • #include<string.h>
    int main()
    int i,j;
    char s[5][80],t[80];
    for(i=0;i<5;i++) // What's this for ?
      scanf("%s",s[i]);     // Why not s[i][j]?
    for(i=0;i<4;i++) // Why is there less than four ?
      for(j=0;j<4-i;j++)    //j<4-i  What do you mean ?
    if(strcmp(s[j],s[j+1])>0) this strcmp() I can't understand why it's written like this ?
     strcpy(t,s[j]);  strcpy(s[j],s[j+1]);  strcpy(s[j+1],t);
    printf("After sorted:\n");
    for(i=0;i<5;i++) // Why do you want one here for loop , Why? i Less than 5 了 ?
    return 0;

Can someone give me a detailed analysis ? I can't understand the program , In addition to the questions I raised above , The boss can have other explanations . Please, please, please , Teach me .

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended