2

This showed up in our laboratory finals examination: Make a program that takes in 10 string inputs into an array. Then outputs the strings in alphabetical order.

I couldn't figure it out during the examination and now I want to know how exactly is it done.

So far this is what I've done. It doesn't work well with similar or equivalent strings, their index gets lost? Anyone can share their solution using only the stdio.h and string.h libraries?

   /*Write a program that takes 10 strings input into an array and outputs them in alphabetical order*/

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

    char strings[10][150];
    char ordered[10][150];
    int i,j,k;
    int ind;

    main()
    {
        printf("INPUT 10 STRINGS\n");
        for(i=0;i<10;i++)
        {
            gets(strings[i]);
        }

        for(i=0;i<10;i++)
        {
            ind=0;
            for(j=0;j<10;j++)
            {
                if(strings[i][0]<strings[j][0])
                {
                    ind++;
                }
                else if(strings[i][0]==strings[j][0])
                {
                    k=0;
                    while((strings[i][k]==strings[j][k])&&strings[j][k+1]!='\0')
                    {
                        if(strlen(strings[i])<strlen(strings[j]))
                        {
                            if(strings[i][k+1]=='\0')
                            {
                                ind++;
                            }
                            else if(strings[i][k+1]<strings[j][k+1])
                            {
                                ind++;
                            }
                        }
                        else if(strlen(strings[i])>strlen(strings[j]))
                        {
                            if(strings[i][k+1]<strings[j][k+1])
                            {
                                ind++;
                            }
                        }
                        k++;
                    }
                }
            }
            strcpy(ordered[ind],strings[i]);
        }


        printf("STRINGS: \n");
        for(i=9;i>-1;i--)
        {
        puts(ordered[i]);
        }
    }
2
  • use strcmp and strcpy Commented Mar 15, 2016 at 18:57
  • 2
    To avoid copy around, I would suggest to declare a array of string pointer. Instead of copying string around, moving pointer around is much faster. Commented Mar 15, 2016 at 18:59

3 Answers 3

2

Just Found a simple way for that:

#include<stdio.h>
#include<string.h>
int main()
{
  int i,j,n;
  char str[20][20],temp[20];
  puts("Enter the no. of string to be sorted");
  scanf("%d",&n);
  for(i=0;i<=n;i++)
  {
      gets(str[i]);
  }
  for(i=0;i<=n;i++)
      for(j=i+1;j<=n;j++)
      {
           if(strcmp(str[i],str[j])>0)
           {
               strcpy(temp,str[i]);
              strcpy(str[i],str[j]);
              strcpy(str[j],temp);
           }
      }
  printf("The sorted string\n");

  for(i=0;i<=n;i++)
  {
      puts(str[i]);
  }
  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

strcmp and strcpy are inbuilt functions defined in string.h

for (i=0; i<10; i++) {
      for (j=0; j<9; j++) {
         if (strcmp(strings[j], strings[j+1]) > 0) {
            strcpy(temp, strings[j]);
            strcpy(strings[j], strings[j+1]);
            strcpy(strings[j+1], temp);
         }
      }
   }

Comments

0

Here's a solution retrieved from here that does what you want but with 5 strings instead. I have adapted it so that it sorts 10 strings instead of 5. All strings have 20 characters at most :

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

void main() {
   char s[10][20], t[20];
   int i, j;
   clrscr();

   printf("\nEnter any five strings : ");
   for (i = 0; i < 10; i++)
      scanf("%s", s[i]);

   for (i = 1; i < 10; i++) {
      for (j = 1; j < 10; j++) {
         if (strcmp(s[j - 1], s[j]) > 0) {
            strcpy(t, s[j - 1]);
            strcpy(s[j - 1], s[j]);
            strcpy(s[j], t);
         }
      }
   }

   printf("\nStrings in order are : ");
   for (i = 0; i < 10; i++)
      printf("\n%s", s[i]);

   getch();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.