tokenizing alphabetically

This is a discussion on tokenizing alphabetically within the C Programming forums, part of the General Programming Boards category; hey guys, finished off most of this code. I believe I should be using strcmp to compare the two newly ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Question tokenizing alphabetically

    hey guys, finished off most of this code. I believe I should be using strcmp to compare the two newly tokenized strings and put the first alphabatized string first.

    Need help with part 4
    example input
    this is a string

    output
    a
    is
    string
    this

    any help is greatly appreciated

    Code:
    #include <stdio.h>
    #include <string.h>
      
    void RecursiveReverse(const char *const sPtr);
      
    int main()
    {
        
      /*Part One*/
      char buffer1 [] = {'T','h','i','s',' ','i','s',' ','a',' ','l','o','o','p','\0'};
      char buffer2 [] = {"This is the second buffer"};
      char buffer3 [80];
      char buffer4 [100];
      char buffer5 [100];
      char *tokenptr;
    //  char *tokenptr1;
      
      gets(buffer3);
      printf("\n%s\n", buffer1);
      printf("%s\n", buffer2);
      
      /*Part 2*/
      char *pb;
      pb = buffer3;
      
      while(*pb != '\0'){
        printf("%c", *pb);
        pb++;
      }
       
      printf("\n");
     
      /*Part Three*/
      printf("The line to print:\n");
      gets(buffer4);
      printf("The new line is:\n");
      RecursiveReverse(buffer4);
      printf("\n");
      
      /*Part 4*/
      printf("Please enter the string to be parsed:\n");
      gets(buffer5);
      printf("\n");
      tokenptr = strtok(buffer5, " ,");                     /*Begins tokenizing*/
      
      while(tokenptr != NULL){
        printf("%s\n", tokenptr);
        tokenptr = strtok(NULL, " ,");                      /*Gets next token*/
      }
      printf("\n");
    //  while(tokenprt != NULL)
      
      return 0;
    }
      
    void RecursiveReverse(const char *const sPtr)
    { 
      if (sPtr[0] == '\0'){
        return;
      }
      else{
      RecursiveReverse(&sPtr[1]);
      
      putchar(sPtr[0]);
      }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see any logic for sorting, but yes, strcmp(string1, string2) is what you want. the return from strcmp() will be 0 if the strings are equal, or the open end of the <, sign, will point toward the string that is greater.

    strcmp(s1, s2) == 0, strings are equal
    strcmp(s1, s2) < 0, means s2 is greater than s1
    strcmp(s1, s2) > 0, means s1 is > s2.

    The actual values strcmp() returns may differ from compiler to compiler, but always the above will be true.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    thanks for the help.
    How would I go about comparing the buffer5 strings?
    Code:
     printf("Please enter the string to be parsed:\n");
      gets(buffer5);
      printf("\n");
      tokenptr = strtok(buffer5, " ,");                     /*Begins tokenizing*/
      
      while(tokenptr != NULL){
        printf("%s\n", tokenptr);
        tokenptr = strtok(NULL, " ,");
    I guess what I mean is, I only put inputs into one string and it gets tokenized. How do I get
    the program to compare two of the tokenized characters.?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( strcmp( placewhereyoucopiedthefirsttokenedstring, placewhereyoucopiedthesecondtokenizedstring ) ... )
        ...
    What part are you having problems with?


    Quzah.
    Last edited by quzah; 02-23-2010 at 04:07 AM. Reason: typo
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One way would be to copy each token string into a 2D char array, one token string per row. Then sort the rows. You could do this using your buffers 2 - 5. Is that what you intended?

    A rather more elegant way would be to make an array of 1 char pointer for each token, and initialize each one (in a loop), to the first letter of each token string. Then sort the pointers, according to what they point at.

    Any way you do it, you need to differentiate each token string from the others, so your sorter can go to work.
    Last edited by Adak; 02-22-2010 at 11:19 PM.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by quzah View Post
    Code:
    if( strcpy( placewhereyoucopiedthefirsttokenedstring, placewhereyoucopiedthesecondtokenizedstring ) ... )
        ...
    What part are you having problems with?


    Quzah.
    I think you want strcmp there, nor strcpy,
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir View Post
    I think you want strcmp there, nor strcpy,
    I've been doing that a lot lately. :/ Too tired. All the time.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    i guess i am having problems understanding how to differentiate each string in buffer5 and knowing how to differentiate them.

    how do i use strcmp for this?

    Code:
     printf("Please enter the string to be parsed:\n");
      gets(buffer5);
      printf("\n");
      tokenptr = strtok(buffer5, " ,");                     /*Begins tokenizing*/
      
      while(tokenptr != NULL){
        printf("%s\n", tokenptr);
        tokenptr = strtok(NULL, " ,");

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The strings have to be differentiated before you can use strcmp(). It has to be able to find the strings before it can make any comparison.

    You have several buffers (char arrays). Can't you put a separate token into each of these buffers?

    It's a klutzy work-around, but it could work for just a few tokens.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    could I essentially do this:?
    Code:
     printf("Please enter the string to be parsed:\n");
      gets(buffer5);
      printf("\n");
      
      tokenptr = strtok(buffer5, " ,;.");                   /*Begins tokenizing*/
        
      while(tokenptr != NULL){
        printf("%s\n", tokenptr);
        tokenptr = strtok(NULL, " ,;.");                    /*Gets next token*/
        }
      
      char *b[20];             /* Begins putting each tokenized string into a array b*/
      
      b[0] = strtok(buffer5, " ,;.");
      
      for(i = 1; i < 20; i++){       
        b[i] = strtok(NULL, " ,;.");
        b[i + 1] = strtok(NULL, " ,;.");
        }
    putting the tokenized strings into the array b. I just cant seem to get it to work. I thought that having the base case on the outside was the way to go but it doesn't work. It only prints the first stored value.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Are you aware that strtok() destroys the string it processes?
    Code:
      tokenptr = strtok(buffer5, " ,;.");                   /*Begins tokenizing*/
        
      while(tokenptr != NULL){
        printf("%s\n", tokenptr);
        tokenptr = strtok(NULL, " ,;.");                    /*Gets next token*/
        }
    I suggest you throw this in after that bit, just to see:

    printf("%s\n",buffer5);

    because it is not what you maybe think it is anymore. That means this next move (processing buffer5 from the beginning again):
    Code:
      char *b[20];             /* Begins putting each tokenized string into a array b*/
      
      b[0] = strtok(buffer5, " ,;.");
    Cannot work.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    k well, I am trying very hard to understand this. Here is another way I tried doing it.
    Code:
    char *b[20];
        
      b[0] = strtok(buffer5, " ,;.");
      
      for(i = 1; i < 20; i++){
        b[i] = strtok(NULL, " ,;.");
        }
    just so i could
    Code:
    if(strncmp(b[i], b[i+1])==0)
    printf("%s\n", b[i]);
    but it seems to tell me segmentation fault core dumped

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    if(strncmp(b[i], b[i+1])==0)
    The possibility of this is dependent on the value of i, and whether b[i+1] exists. You may be running off the end of your array.

    Post all the code as you are using it now, from the declarations up to the strcmp.

    Also -- make sure you have:
    #include <string.h>
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    Okay, I now know what the problem is
    Code:
      /*Part 4*/
      printf("Please enter the string to be parsed:\n");
      gets(buffer5);
      printf("\n");
    
      
    char *b[20];
        
      b[0] = strtok(buffer5, " ,;.");
      
        for(i = 0; i < 2; i++){
        b[i+1] = strtok(NULL, " ,;.");
        printf("%s\n", b[i]);
        }
    The red is the problem, it dumps it if I don't input 2 strings. How do I get around that?

    When I put in two inputs works no problem
    Sample Input:
    asdf. asdf
    Sample Output:
    asdf
    asdf

    When i put in 1 input:
    asdf
    output:
    a

    a
    Segmentation fault (core dumped)

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Check:
    Code:
    if (b[i]) printf("%s\n", b[i]);
    else puts ("NULL token");
    printf will segfault if you try to print a non-existent string (strtok will return NULL if it does not get anything).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 3 123 LastLast
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. 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, 01:30 AM
  4. Problem with string tokenizing
    By Mr_Miguel in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 01: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


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