I am having a problem in sorting by name. for eg: if given two names Sameer and Sean, I want to print Sameer and not Sean. It needs to check character by character. I have to extend this to a greater no. of names but I need to get the basic right first. Here is the code i have written:
#include<string.h>
#include<conio.h>
#include<stdio.h>
int main(void)
{
clrscr();
int flag,x=0;
char name1[20],name2[20],name3[20],ch,*ptr,*ptr2;
gets(name1);
gets(name2);
flag=0;
ch=97;
for(x=0;x<(strlen(name1)) && x<(strlen(name2)) && flag==0;x++)
{
ch=97+x;
ptr = strrchr(name1,ch);
ptr2 = strrchr(name2,ch);
if(ptr==ptr2)
{
flag=0;
}
if(ptr==0 && ptr2!=0)
{
strcpy(name3,name1);
flag=1;
}
if(ptr2==0 && ptr!=0)
{
strcpy(name3,name2);
flag=1;
}
if(ptr<ptr2)
{
strcpy(name3,name1);
}
if(ptr>ptr2)
{
strcpy(name3,name2);
}
}
printf(" %s",name3);
getch();
return 0;
}
Some one pls help me out!