Thread: tokenizing alphabetically

  1. #31
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    input in red

    Displaying contents of *b[]dd
    aa
    cc
    e
    f
    b


    press enter to continue
    aa
    b
    cc
    dd

    e

  2. #32
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try it with the new sort code. (I changed it in the earlier post). Keep in mind that capital letters will always sort out lower than any lower case letters. That can throw you if you're used to case insensitive sorting.

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The problem that I see is that your *b[] has "ragged" amounts of char space, for each word.

    So b[0] has 5 chars (4 + end of string)
    but b[2] only has 2 chars in it.

    So when you start sorting, you're toast. b[2] has no room for more than 2 chars in it. You can test this for yourself. Put equal length strings in all of *b[]. The sort works great now. Once you start varying the length of the strings, it's goofed.

    Could I interest you in a little index sort, with the same sorter? It would leave everything in place, but it can appear (and be accessed in), sorted order.

    Bit of magic there, if you like.
    Last edited by Adak; 02-25-2010 at 01:32 PM.

  4. #34
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    yes that would good to see. The more info I learn the better.
    How would I put *b[] into equal lengths? Would there be a set way of doing it?

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Enjoy! Works fine.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i, j, temp_num; 
      int index[6];
      char temp[20];
      char *str[6] = {
      {"This"}, 
      {"is"}, 
      {"a"}, 
      {"string"}, 
      {"to"}, 
      {"parse"}
      };       // scanf("%s", str); 
      for(i = 0; i < 6; i++) {
        printf("\n%s %d", str[i], strlen(str[i])); 
      }
      //initialize the index array
      for(i = 0; i < 6; i++)
        index[i] = i;
      
      printf("\n\n");
      for(i = 0; i < 6; i++) {
        for(j = i+1; j < 6; j++) {
    
                         //normal string sorting:
                         //if((strcmp(str[i], str[j])) > 0) {
                            //strcpy(temp, str[i]);
                            //strcpy(str[i], str[j]);
                            //strcpy(str[j], temp);
    
          //sorting by index:
          if((strcmp(str[index[i]], str[index[j]])) > 0) {
            temp_num = index[i];
            index[i] = index[j];
            index[j] = temp_num;
          }
        }
      }
    
      printf("\n\n In Sorted Order:");
      for(i = 0; i < 6; i++)  //print using the index
        printf("\n%s", str[index[i]]);
    
      printf("\n\n Order in the array is unchangedl: ");
      for(i = 0; i < 6; i++) {
        printf("\n%s %d", str[i], strlen(str[i])); 
    
           
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar();
      return 0;
    }
    You would have to use a regular 2D array: b[6][7], or allocate the memory.
    Last edited by Adak; 02-25-2010 at 03:21 PM.

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, 04:53 AM
  2. Arrange letters of the string alphabetically
    By ama_cuber in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 03:40 AM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  4. Problem with string tokenizing
    By Mr_Miguel in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 02:02 PM
  5. Inserting alphabetically into a Linked List
    By EDL in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 06:51 AM

Tags for this Thread