write a program to get a 2 d character array and arrange them in alphabetical order.

i've tried it,but its not getting compiled.
im jus a beginner.so, someone plz help me.
given below is my program....

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

struct st{

       char a[5][10];
       }ppl;



int main()
{
    int i,j;
    char b[10];

    for(i=0;i<5;i++)
    {
                    printf("enter the name of the %d person :",i+1);
                    scanf("%s",ppl.a[i]);
}

printf("the names after arranging in alphabetical order:\n\n");


for(i=0;i<5;i++)
{
 for(j=0;j<5;j++)
 {
  if(ppl.a[i][1]>ppl.a[i+1][1]) 
  {
   b=ppl.a[i];
   ppl.a[i]=ppl.a[i+1];
   ppl.a[i]=b;
}
}}
for(i=0;i<5;i++)
{
                printf("the name of the %d person is %s\n",i+1,ppl.a[i]);
}

getch();
return 0;
}

If you want sorted strings, then you have to compare them as strings, not as individual char's. You can see the problem:

Abbey should sort out lower than Cathy, but when you are comparing the 'b' in the second letter of Abbey, with the 'a' in the second letter of Cathy, it won't.

You need to #include <string.h>, and then use strcmp(string1, string2).

If strcmp() returns > 0, then string1 is higher (greater) than string2. If strcmp() returns 0, the strings are equal, and if strcmp() returns < 0, then string1 < string2.

in pseudo code:

if((strcmp(string1, string2)) > 0) 
  string1 > string2 so make your swap code
  right in here
end if
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.