Like Tree1Likes
  • 1 Post By iMalc

Sorting strings within an array

This is a discussion on Sorting strings within an array within the C Programming forums, part of the General Programming Boards category; This this is what I have so far. I am trying to sort these name alphabetically. So the output should ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    14

    Sorting strings within an array

    This this is what I have so far. I am trying to sort these name alphabetically. So the output should be "Barney, Dino, Fred, Pebbles, Dino"
    Also I have to use strcmp.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    main ()
    {
            char str[10][10];
            int i = 0; 
            int j = 0;       
            int temp = 0;
            
            strcpy(str[0], "Fred");
            strcpy(str[1], "Barney");
            strcpy(str[2], "Wilma");
            strcpy(str[3], "Pebbles");
            strcpy(str[4], "Dino");
    
    
    
            for(i = 0;i < 5;i++){
            printf("%s,", str[i]);
            }
    
    
            printf("\n\n");
           system("PAUSE");
           return 0;    
    }
    I have done something similar with numbers, but never with strings.

    Code:
    int main (){
    
    
            int arr[10] = {94, 36, 19, 8, 62, 49, 21, 53, 1, 88};
            int i = 0; 
            int j = 0;       
            int temp = 0;
    
    
            for(i = 0;i < 10;i++){
            for(j = 0; j < 9;j++){
                if(arr[j] > arr[j+1]){
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                }
            }        
            }
            for(i = 0;i < 10;i++){
            printf("%d,", arr[i]);
            }
            printf("\n\n");
           system("PAUSE");
           return 0;    
    }
    I would appreciate any ideas or types. So far I have only been able to move individual letters instead of the entire names. Thanks.
    Last edited by tictac232434; 11-03-2012 at 11:56 AM.

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    To compare strings you need a specialized function: strcmp().
    To assign one string to another, use strcpy().

    With the necessary changes in your second code from comparing and copying integers to strings, your first code should work for strings.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    14
    I am getting close just need a bit of help...

    Code:
             for(i = 0;i < 10;i++){        for(j = 0; j < 10;j++){
                if(strcmp(str[j], str[j+1])){
                temp = str[j][i];
                str[j][i] = str[j+1][i];
                str[j+1][i] = temp;
                }
            }   
            }
    Last edited by tictac232434; 11-03-2012 at 12:44 PM.

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    strcmp() compares the two strings it receives and returns

    + a negative number if the first comes before (is smaller than) the second
    + zero if the strings are equal
    + a positive number if the first comes after (is greater than) the second

    Try
    Code:
    if (strcmp(str[j], str[j + 1]) > 0) { /* ... */ }

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    14
    Its moving the first letters of each name. Is there a way to move the full names?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Notice how in your int sorting version, 'temp' was the same type as one of the items in your array?
    Well the items in your array are now an array of 10 chars. So temp needs to be an array of 10 chars.

    You could use strcpy to copy those 10 chars over in the three actions that constitute a swap.

    You also had your j loop nicely only going up to <9 so that j+1 didn't cause a buffer overrun, but then you chnaged it to <10 in your string version. You should reconsider that.
    tictac232434 likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by qny View Post
    To assign one string to another, use strcpy().

    Quote Originally Posted by tictac232434 View Post
    Its moving the first letters of each name. Is there a way to move the full names?
    Reread my previous post
    Use strcpy()

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    14
    I appreciate the help iMalc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help sorting my array of strings
    By Infamous in forum C Programming
    Replies: 19
    Last Post: 03-10-2011, 10:46 PM
  2. sorting an array of strings
    By porstart in forum C Programming
    Replies: 3
    Last Post: 02-22-2011, 09:43 PM
  3. Sorting an array of strings
    By porstart in forum C Programming
    Replies: 2
    Last Post: 11-21-2010, 08:46 PM
  4. Sorting Array of Strings
    By nair in forum C Programming
    Replies: 6
    Last Post: 10-09-2010, 11:10 AM
  5. Sorting strings in an array
    By JLan in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 04:10 PM

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