Thread: Alphabetizing Names

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    10

    Alphabetizing Names

    I'm being asked to do things I haven't done in 2 years, so naturally, I'm rusty. Basically I need to alphabetize 5 names, Susan, Ralph, Marie, Bob, and George. What I want to do is store is name in a string, and compare the first letter in each name, and then print them in that order. This is what I have so far:
    Code:
     
    #include <stdio.h>
    
    int main (void)
    {
    char name1[15];
    char name2[15];
    char name3[15];
    char name4[15];
    char name5[15];
    printf("Enter name 1");
    scanf("&#37;14s",name1);
    printf("Enter name 2");
    scanf("%14s",name2);
    printf("Enter name 3");
    scanf("%14s",name3);
    printf("Enter name 4");
    scanf("%14s",name4);
    printf("Enter name 5");
    scanf("%14s",name5);
    return 0;
    }
    So I know how to store the info, I just have no Idea how to sort it. I'm not asking for anyone to do the rest for me, just need a little help on the method.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should look up strcmp.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    10
    Ok, I redid it
    Code:
    #include <stdio.h>
    void sort(char *array[], int size);
    
    int main()
    {
    int i;
    int asize=5;
    char *names[5]={"Susan","Ralph","Marie","Bob","George"};
    
    sort(names,asize);
    
    for(i=0;i<5;i++)
    {
    printf("%s\n", *(names +i));
    }
    return 0;
    }
            
             
                    
                    
    void sort(char *array[], int size)
    {
    int final;
    int j;
    int k;
    for(j=0;j<size-1;j++)
    {
            for(k=0;k<size-1-j;k++)
            {
                    final= strcpm(array[k],array[k+1]);
                    if (final>0)
                            swap(array[k],array[k+1]);
            }
    }
    }
    the error i get is

    main.o(.text+0xe1): In function `sort':
    : undefined reference to `strcpm'
    main.o(.text+0x11d): In function `sort':
    : undefined reference to `swap'
    collect2: ld returned 1 exit status



    is there some kind of header file i need to use strcmp and swap?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    #include <string.h>

    Learn to spell strcmp, that sort of thing matters
    The compiler wont make an educated guess

    Write yourself a swap function

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    10
    Quote Originally Posted by citizen View Post
    #include <string.h>

    Learn to spell strcmp, that sort of thing matters
    The compiler wont make an educated guess

    Write yourself a swap function
    how would I do that?

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    10
    nvm, i just figured it out and it works. It was warning me about not casting however...can any specify on what exactly this means.

    Code:
    include <stdio.h>
    #include <string.h>
    void sort(char *array[], int size);
    
    int main()
    {
    int i;
    int asize=5;
    char *names[5]={"Susan","Ralph","Marie","Bob","George"};
    
    sort(names,asize);
    
    for(i=0;i<5;i++)
    {
    printf("%s\n", *(names +i));
    }
    return 0;
    }
    
    void sort(char *array[], int size)
    {
    int final;
    int j;
    int k;
    int temp;
    for(j=0;j<size-1;j++)
    {
            for(k=0;k<size-1-j;k++)
            {
                    final= strcmp(array[k],array[k+1]);
                    if (final>0)
                            {
                                    temp= array[k];
                                    array[k]=array[k+1];
                                    array[k+1]=temp;
                            }
            }
    }
    }
    Thanks for the help all

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That's a decent try, but temp should be a pointer to a string, since that's what it will be holding onto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. runtime function names
    By lruc in forum Tech Board
    Replies: 2
    Last Post: 10-11-2008, 10:51 AM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 10:34 AM
  4. Reading File Names
    By Thrack in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2007, 05:08 PM
  5. Opening files with korean file names
    By tek in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 10:22 PM