Alphabetical Order HELP! dont understand what im doing wrong

This is a discussion on Alphabetical Order HELP! dont understand what im doing wrong within the C Programming forums, part of the General Programming Boards category; I already have this code and I need help figuring out how to take in a .txt file and printing ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Alphabetical Order HELP! dont understand what im doing wrong

    I already have this code and I need help figuring out how to take in a .txt file and printing it out in alphabetical order. I dont understand how to use the strcmp function. Please help!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_LENGTH 40
    
    void sort(char names[][MAX_LENGTH], int size);
    void print(char names[][MAX_LENGTH], int size);
    int my_strcmp(const char* s1, const char* s2);
    void swap(char names[][MAX_LENGTH], int i, int j);
    void my_strcpy(char* s1, const char* s2);
    
    int main() {
        
        char filename[MAX_LENGTH];
        FILE *fin;
        int i;
         
        // Get the name of the file and open it.
        printf("What file stores the input names?\n");
        scanf("%s", filename); 
        fin = fopen(filename,"r");
        
        // Find out the number of names.
        int n;
        fscanf(fin, "%d", &n);
        char names[n][MAX_LENGTH];
        
        // Read in all of the names.
        for (i=0; i<n; i++)
            fscanf(fin, "%s", names[i]);
        
        sort(names, n);
        printf("Here is the list of names:\n");
        print(names, n);
        
        fclose(fin);
        system("PAUSE");
        return 0;
    }
    
    
    void sort(char names[][MAX_LENGTH], int size) {
    
        int i,j;
        
        // How far we have to go in the array on each iteration.
        for (i=size-1; i>0; i--) 
        
            // Just swap any adjacent elements that are out of order.
            for (j=0; j<i; j++)
                if (my_strcmp(names[j], names[j+1]) > 0)
                    swap(names, j, j+1);            
    }     
    
    
    void swap(char names[][MAX_LENGTH], int i, int j) {
        char temp[MAX_LENGTH];
        my_strcpy(temp, names[i]);
        my_strcpy(names[i], names[j]);
        my_strcpy(names[j], temp);     
    }
         
    
    void print(char names[][MAX_LENGTH], int size) {
    
        int i;
        for (i=0; i<size; i++)
            printf("%s\n", names[i]);
    }     
    
    
    int my_strcmp(const char* s1, const char* s2) {
    
      
    }
    
    
    void my_strcpy(char* s1, const char* s2) {
    int i;
    
       for ( i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) {
          ;   
       } 
    }

  2. #2
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    strcmp uses the zero value for equaliy, for instance (strcmp(a,b)==0) if they are both equal in every aspect zero will be returned, now if they are different ull have a more than or less than value.

    tsk!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Isn't that what I am doing here?
    Code:
        // Just swap any adjacent elements that are out of order.
            for (j=0; j<i; j++)
                if (my_strcmp(names[j], names[j+1]) > 0)
                    swap(names, j, j+1);

  4. #4
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by quinn22 View Post
    Isn't that what I am doing here?
    Code:
        // Just swap any adjacent elements that are out of order.
            for (j=0; j<i; j++)
                if (my_strcmp(names[j], names[j+1]) > 0)
                    swap(names, j, j+1);
    Umm my_strcmp ??????????????? i dnt seee anything in that function Umm btw i didnt read all the codes, just taking a peek thats all, still though
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Provided you had written my_strcmp, so that it returned something meaningful?

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    That's what I dont understand, in the books I'm reading the function doesn't have anything in it. I figured thats what the #include <string.h> is for.

  7. #7
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by quinn22 View Post
    That's what I dont understand, in the books I'm reading the function doesn't have anything in it. I figured thats what the #include <string.h> is for.
    lol, if the function has nothing it will do nothing right bla, replace it with the strcmp from the string lib. Umm read boy!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  8. #8
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    Provided you had written my_strcmp, so that it returned something meaningful?
    Follow Tabstop's advice he's a pro im a rookie!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  9. #9
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Rewriting C libraries is one of the dumbest things you can do.

    Use strcmp(). Unless your professor explicitly told you to write your own implementation.

  10. #10
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by MeTh0Dz View Post
    Rewriting C libraries is one of the dumbest things you can do.

    Use strcmp(). Unless your professor explicitly told you to write your own implementation.
    Yup exactly, umm Methodz did u go over the stuff i sent.. tsk, just asking
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Speaking of going over stuff, Matus, did you get your code back that I had sent you with many many many comments and a few changes, and a couple recommendations?

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually..... Hold up, I didn't look at your insert function until now and it is funky.

  13. #13
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    Actually..... Hold up, I didn't look at your insert function until now and it is funky.
    Sorry matt, just opened it, didnt know it was there, cuz i had checked earlier. i'll take a look
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok cool. I would insert alphabetically, if I were you. The nice thing about circular linked lists is you can scan through them bidirectionally with easy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arranging a file into alphabetical order.
    By Malachi in forum C Programming
    Replies: 18
    Last Post: 02-10-2009, 11:07 PM
  2. names in alphabetical order?
    By n3cr0_l0rd in forum C Programming
    Replies: 21
    Last Post: 02-06-2009, 08:59 PM
  3. Wrong order when executing?
    By R@m0n in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2002, 06:04 AM
  4. Help me sort the names in alpha order.
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 02:30 PM
  5. Alphabetical order
    By BubbleMan in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21