Hello!
I have written this code which is supposed to take five names as input and then arrange them in alphabetical order. It got compiled easily but after I input those 5 names, the window closes automatically even when I have getch() in place. Also, I don't think I have written the function which sorts the names [word_swap()] any good.
Please help me set this code right....(I know ive made a mess, sorry for that :p )
Thanks!
Code:#include<stdio.h>
#include<conio.h>
void word_swap(char*,char*); /*I don't think I have written the definition of this function correctly*/
int main()
{
char temp[20],names[5][20];
int x,y,z;
for(x=0;x<5;x++)
{
printf("\nEnter a name :");
scanf("%s",&temp);
for(y=0;names[x][y]!='\0';y++)
{
names[x][y]=temp[y];
}
names[x][y]='\0';
}
for(x=0;x<5;x++)
{
for(z=x+1;z<5;z++)
{
for(y=0;names[x][y]!='\0'&& names[z][y] !='\0';y++)
{
if((int)names[x][y]>(int)names[z][y])
word_swap(&names[x][0],&names[z][0]);
}
}
}
for(x=0;x<5;x++)
{
printf("\n");
for(y=0;names[x][y]!='\0';y++)
printf("%c",names[x][y]);
}
getch();
return 0;
}
void word_swap(char*str1,char*str2)
{
int n=0;
char temp;
while(n!=2)
{
temp=*str1;
*str1=*str2;
*str2=temp;
str1++;
str2++;
if(*str1=='\0')
n++;
if(*str2=='\0')
n++;
}
}

