sorting alphabetically

This is a discussion on sorting alphabetically within the C Programming forums, part of the General Programming Boards category; oki need to sort a string alphabetically by the method of selection sort. here's the piece of code i don't ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    sorting alphabetically

    oki need to sort a string alphabetically by the method of selection sort.

    here's the piece of code i don't quit understand.

    Code:
    void sortString(char* s, int size) 
    {
      int i, j;
      char current;
     
      for (j = 1; j < size; j++) {
        current = s[j];
        i = j;
     
        while (i > 0 && current < s[i-1]) {
          s[i] = s[i-1];
          i--;
        }
        s[i] = current;
      }
    }
    i think i understand the for loop. but the while function is confusing me. current is of char type and i guess so is the string 's[i-1]. i thought you couldn't compare char's or strings. unless it was done by ACSII, though i'm not how it all works.
    Last edited by ahming; 05-25-2005 at 06:40 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    i thought you couldn't compare char's or strings. unless it was done by ACSII
    Not quite. A character is basically a tiny int. When you use something like 'A' (character in single quotes) it translates it to a number. In ASCII it would use 65 for 'A'. So if you do:
    Code:
    char c = 'A';
    the c variable holds the value 65. So if you do c1 < c2 you're checking if the first character is less than second character. Since alphabetic characters are placed in the ASCII table in ascending order it makes it a convenient way to see if one character comes before another alphabetically.

    In your example, since s is of type char *, then s[index] ( the same as *(s + index) ) is of type char.

    Does that make a little more sense?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    the same as *(s + index)
    don't understand that bit.. sorry.

    but everything else you said made perfect sense. Since the single quotation marks make the letter A, equal to 65. say in the string it currently holds the word 'post'. now s[0] being 'p' and s[3] being 't'. since there are no quotation marks around s[i] or current, how does it compare the two characters?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    since there are no quotation marks around s[i] or current, how does it compare the two characters?
    Because s[whatever_index] and current already hold the numerical value of the character. if you did strcpy(s, "hello"); and looked at s in memory it would look like this:
    Code:
    104 101 108 108 111 0
    Doing current = 'A'; is exactly equivalent to doing current = 65; as long as you're on a machine using ASCII. Both of those statements store 65 in current.

    Maybe this program will help you understand:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char c = 'A';
    
      printf("char = %c, value = %d\n", c, c);
    
      return 0;
    }
    You get:
    Code:
    char = A, value = 65
    Two ways of displaying the same variable.
    Last edited by itsme86; 05-26-2005 at 07:52 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    yeah i understand the ascii

    how do i put it..
    well lets use 'post' again..
    char letter = 'B' (which equals 66)
    char string[i] (holds post)

    if letter equals 66
    string[0] is p. which 112 on the ascii table

    so your saying that if i do

    Code:
    if(string[0] < letter)
    this will return false? it will be read as
    Code:
    if(112 < 66)

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Exactly.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    oh didn't know you could do that.. thought you had to use string functions from the string library. such as strcmp or whatever the one is for comparing strings.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    But you're not comparing strings. You're comparing characters. For instance:
    Code:
    if("foo" < "bar")
    is not correct. That compares the pointer to the first string with the pointer to the second string. In that case you need to use something like strcmp() instead which tests the actual characters in the string. Something like:
    Code:
    if(str1[0] == str2[0] && str1[1] == str2[1])
    etc., etc.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    aahhh okok thanks a lot for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an array alphabetically?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 03-09-2011, 03:53 AM
  2. Sorting an array alphabetically - split
    By wurzull in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 11:17 PM
  3. Sorting Alphabetically
    By orgy230 in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 01:28 PM
  4. Sorting inputed letters alphabetically...
    By IanelarAzure in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2002, 08:56 PM

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