Connecting Tech Pros Worldwide Forums | Help | Site Map

sorting problem

Newbie
 
Join Date: Oct 2006
Posts: 27
#1: Dec 6 '06
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>#include <string.h>
  2. int compare (char *s,char *a)
  3. {
  4.     int d;
  5.     for (;*s&&*a;*s++,*a++)
  6.     {
  7.         if (*s==*a){d=0;}
  8.         else d=*s-*a;
  9.  
  10.     }
  11.     return d;
  12. }
  13. void copy (char *s,char *a)
  14. {
  15.     for(;*s=*a;s++,a++);
  16. }
  17. int main()
  18. {
  19.        char  tname[20], name[20][20];
  20.        int   i, j, n;
  21.        printf("Enter the number of names:  ");
  22.        scanf("%d",&n);
  23.        for(i=0; i<n; i++)
  24.        {
  25.               printf("\nEnter the name%d:",i+1);
  26.               scanf("%s",name[i]);
  27.        }
  28.        for(i=0; i<n-1; i++){
  29.            for(j=1;  j<=n-1; j++){
  30.        if(compare(name[i], name[j])>0)
  31.        {
  32.               copy(tname, name[i]);
  33.               copy(name[i], name[j]);
  34.               copy(name[j], tname);
  35.        }
  36.            }
  37.        }
  38.        printf("\nSorted names:\n");
  39.        for (i =0; i<n; i++)
  40.        printf("%s\n",name[i]);
  41.            return 0;
  42.  
  43. }
  44.  
i wrote this program it must sort enterd names but it has some problems. please help me fix it.

Reply