Array of strings sorted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teamphil06
    New Member
    • Feb 2007
    • 4

    Array of strings sorted

    Code:
    #include<string.h>
    main (void){
    	char name[20][20];
    	int i;
    	clrscr();
    	for(i=0;i<=4;i++){
    		printf("Enter the Names:\n",i);
    		scanf("%s",&name[i]);
    		}
    
    		//sorting//
    		
           for(i=0;i<=4;i++){
    		printf("%s\n",name[i]);
    		}
    	getch();
    	return 0;
    }
    here's my running program, but my instructor want it to be sorted alphabetically. .. so someone help me with this...
    its 2 dimensional array of strings but not yet sorted...
    pls give me some function how to sort it by alphabetical order...
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    can you use the sort library funtion, e.g.
    http://www.cplusplus.c om/reference/clibrary/cstdlib/qsort.html

    or do you have to write you own?

    you can use function strcmp() to compare a pair of strings
    http://www.cplusplus.c om/reference/clibrary/cstring/strcmp.html

    Comment

    • teamphil06
      New Member
      • Feb 2007
      • 4

      #3
      Code:
      #include<string.h>
      main (void){
      	char name[20][20];
      	int i,x;	
      	char hold;
      	clrscr();
      		for(i=0;i<=4;i++){
      		printf("Enter the Names:\n",i);
      		scanf("%s",&name[i]);
      		}
      
      		//sorting//
      	
      
      		for(i=0;i<=4;i++){
      		  for(x=i;x<=4;x++){
      			name[i]="abc";
      			hold[x]="afs";
      		    if(name[i]>hold[x]){
      			 printf("%s\n",name[i]);
      		    else
      			printf("%s\n",hold[x]);
      			}
      		    }
      		 }
             for(i=0;i<=4;i++){
      		printf("%s\n",name[i]);
      		}
      	getch();
      	return 0;
      }

      anybody can sort by this by alphabetical order??
      to sort pls start at the statement //sorting//

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by teamphil06
        #include<string .h>
        main (void){
        char name[20][20];
        int i,x;
        char hold;
        clrscr();
        for(i=0;i<=4;i+ +){
        printf("Enter the Names:\n",i);
        scanf("%s",&nam e[i]);
        }

        //sorting//


        for(i=0;i<=4;i+ +){
        for(x=i;x<=4;x+ +){
        name[i]="abc";
        hold[x]="afs";
        if(name[i]>hold[x]){
        printf("%s\n",n ame[i]);
        else
        printf("%s\n",h old[x]);
        }
        }
        }
        for(i=0;i<=4;i+ +){
        printf("%s\n",n ame[i]);
        }
        getch();
        return 0;
        }


        anybody can sort by this by alphabetical order??
        to sort pls start at the statement //sorting//
        No one is going to write your code for you. Horace gave you the tool that you need to compare two strings, why don't you try to use it to compare a few strings and see if you can determine how you might be able to tell which one comes before the other in alphabetical order.

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by RedSon
          No one is going to write your code for you. Horace gave you the tool that you need to compare two strings, why don't you try to use it to compare a few strings and see if you can determine how you might be able to tell which one comes before the other in alphabetical order.
          You should also state what you ment by sorting a 2D array as this could mean a few different things. Also describe any restrictions and post your solution (working or not) so that we can see that you are trying.


          Adrian

          Comment

          • teamphil06
            New Member
            • Feb 2007
            • 4

            #6
            thats it, i don't know how to do it the sorting part, thats y i need your help...

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              I assume you are sorting the 2D char array (char name[20][20]) as above.

              Then I would assume this means that as you increase the first index (name[0], name[1], ... , name[19]), the strings stored in the second index (name[0][0] through name[0][19]) should be getting lexicographical ly larger, or alphabetically larger. If you have "Animal" and "Baseball", then "Baseball" is lexicographical ly larger - thus, "Baseball"s index will be greater than "Animal"s.

              Comment

              Working...