current position:Home>If you enter the title of a book, the length of each title is no more than 100, and the books are sorted in ASCII order from small to large. Enter the number of book titles indefinitely until the end of the document. Sort the string from small to large ac

If you enter the title of a book, the length of each title is no more than 100, and the books are sorted in ASCII order from small to large. Enter the number of book titles indefinitely until the end of the document. Sort the string from small to large ac

2022-02-02 23:02:06 CSDN Q & A

#include"stdio.h"#include"string.h"int main(){    int n,j,i,j1,k;    char a[100][100],t[100];                   while(scanf("%s",a[n])!=EOF)       {           n++;       }    for(i=0;i<n-1;i++)    {            for(j1=i+1;j1<n;j1++)        {            if(strcmp(a[i],a[j1])>0)            {        strcpy(t,a[i]);        strcpy(a[i],a[j1]);        strcpy(a[j1],t);            }    }        }  for(i=0;i<n;i++)  {      printf("%s\n",a[i]);  }              return 0;}



Refer to the answer 1:

In the code n No initialization value , Revised as follows , For reference :

#include <stdio.h>#include <string.h>int main(){    int  n = 0, j, i;    char a[100][100], t[100];    while (gets(a[n]) != NULL) //ctrl+Z  End input     {        n++;    }    for (i = 0; i < n - 1; i++)    {        for (j = i + 1; j < n; j++)        {            if (strcmp(a[i], a[j]) > 0)            {                strcpy(t, a[i]);                strcpy(a[i], a[j]);                strcpy(a[j], t);            }        }    }    for (i = 0; i < n; i++)    {        printf("%s\n", a[i]);    }    return 0;}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022302049658.html

Random recommended