tokenizing alphabetically

This is a discussion on tokenizing alphabetically within the C Programming forums, part of the General Programming Boards category; awesome, I took out the Code: else puts ("NULL token"); Thank you so much....

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    awesome, I took out the
    Code:
    else puts ("NULL token");
    Thank you so much.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    Here is my full code. The red area is where the tokenized strings should be checked for comparability by the first character. Giving me same problem.
    THe last thing i need and am trying to figure out is the strncmp to get it to put

    hello world, a cool thing
    into

    a
    cool
    hello
    thing
    world

    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;
      int i;
      
      
      printf("print a line:");
      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");
    
      
    char *b[20];
        
      b[0] = strtok(buffer5, " ,;.");
      
      for(i = 0; i < 20; i++){
      b[i+1] = strtok(NULL, " ,;.");
      if (b[i]) printf("%s\n", b[i]);
        //else puts ("NULL token");  
        }
      
        //for(i = 0; i < 2; i++){
        //(strncmp(b[i], b[i+1], 1))
        //printf("%s\n", b[i]);
        //}
      
      return 0;
    }
    
    void RecursiveReverse(const char *const sPtr)
    {
      if (sPtr[0] == '\0'){
        return;
      }
      }
      else{
      RecursiveReverse(&sPtr[1]);   
      
      putchar(sPtr[0]);
      }  
    }

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need to keep a count of how many actual tokens you get. After the first time strtok() returns NULL, it only keep only doing so -- there's clue #1.

    If you break out of the for() loop at that point (via the "else"), i will equal the number of tokens (ie, one more than the last non-null index of b).

    If you use a different variable to control the second loop, you can count upto the current value of i-1 doing the strcmp (or actually, i-2 since you want to compare the second last to the last).

    Alternatively, you could use:
    Code:
    i = 0; // reset
    while (b[i+1]) {  // next element is still non-NULL
        [strcmp stuff] 
        i++;
    }
    Last edited by MK27; 02-24-2010 at 09:05 AM.
    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

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    got it to work somewhat. It will post both just won't put in order and post repeats instead of the actual characters. won't post last character either....?
    Thought blue might be needed for base case.


    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;
      int i;
      
      
      printf("print a line:");
      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");
    
      
    char *b[20];
        
      b[0] = strtok(buffer5, " ,;.");
      
       for(i = 0; i < 20; i++){
      b[i+1] = strtok(NULL, " ,;.");
      if (b[i]) printf("%s\n", b[i]);
        else puts ("NULL TOKEN");
        }
      
      i = 0;
      //if(strcmp(b[i], b[i+1])==0)
     // printf("%s\n", b[i]);
      
      while(b[i+1]){
        if(strcmp(b[i], b[i+1]) == 0)
          printf("%s\n", b[i]);
         else
           if(strcmp(b[i], b[i+1]) < 0)
             printf("%s\n", b[i]);
             else
               if(strcmp(b[i], b[i+1]) > 0)   
                 printf("%s\n", b[i+1]);
       i++;
      }
    
      
      return 0;
    }
    
    void RecursiveReverse(const char *const sPtr)
    {
      if (sPtr[0] == '\0'){
        return;
      }
      }
      else{
      RecursiveReverse(&sPtr[1]);   
      
      putchar(sPtr[0]);
      }  
    }
    here is output:
    Code:
    sol:~>a.out
    print a line:ok
    
    This is a loop
    This is the second buffer
    ok
    The line to print:
    okay
    The new line is:
    yako
    Please enter the string to be parsed:
    b.a.c
    
    b
    a
    c
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    NULL TOKEN
    a
    a
    Last edited by patso; 02-24-2010 at 10:09 AM. Reason: show the code outputted

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm not sure what you want to do there. You should replace that "NULL TOKEN" bit with:
    Code:
    else break;
    This will end the loop, since there is no point continuing after that (hint: so you could use a while() condition here instead of the for loop, too).

    wrt, maybe it will help to look at it indended more conventionally:
    Code:
    while(b[i+1]) {
    	if(strcmp(b[i], b[i+1]) == 0)
    		printf("%s\n", b[i]);
    	else if(strcmp(b[i], b[i+1]) < 0)
    		printf("%s\n", b[i]);
    	else if(strcmp(b[i], b[i+1]) > 0)   
    		printf("%s\n", b[i+1]);
    	i++;
    }
    You should just call strcmp() once and assign the return value to a variable and use that. Anyway, so you are saying if the strings match, print the first one. If they don't and i < i+1, print the first one. If they don't and i > i+1, print the second one.
    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

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    i was just trying that to see if it would work.

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    i have been told that I could use a bubble sort in conjunction with strcmp to get it to sort alphabetically.

    I don't know how to go about putting the strcmp into a bubble sort.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can use any sorting algorithm in conjunction with strcmp.

    This is selection sort (very close to bubble sort)

    Code:
    char temp[MAX]; //MAX = largest element of data in array
    
    
    
    for(i = 0; i < MAX; i++) {
      for(j = i+1; j < MAX; j++) {
        if((strcmp(str1, str2)) > 0) {  //str1 > str2, so we need to swap
           strcpy(temp, str1);
           strcpy(str1, str2);
           strcpy(str2, temp);
        }
      }
    }
    This was done impromptu, so if there's an error, just let me know if you can't fix it.

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Unhappy

    It works for the desired number 4. But if I go under that or over it gives me a segmentation fault. How could I get strlen in there when it is being tokenized to determine the amount of tokenized words and use that count in my swap?

    Sample input:
    this is a sample

    Output:
    the number of words is 4
    a
    is
    sample
    this

    The orange text is easy to print out I just used it to see if the function would work. I will use a for loop later for the amount.


    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 < 20; i++){
      b[i+1] = strtok(NULL, " ,;.");
      if (b[i]) printf("%s\n", b[i]);
        else break;
        }
    
      printf("\n\n");
    
      char temp[20]; 
        
      int j;
      for(i = 0; i < 4; i++) {
        for(j = i+1; j < 4; j++) {
          if((strcmp(b[i], b[i+1])) > 0) {  //str1 > str2, so we need to swap
            strcpy(temp, b[i]);
            strcpy(b[i], b[i+1]);
            strcpy(b[i+1], temp);
          }
        }
      }
    
    printf("%s\n%s\n%s\n%s\n", b[0], b[1], b[2], b[3]);

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    okay, just a few bugs and it is done.

    as soon as I go over three words, it won't alphabetize them....

    Code:
      printf("Please enter the string to be parsed:\n");   
      gets(buffer5);
      printf("\n");
        
      char *b[20];
    
    
      b[0] = strtok(buffer5, " ,;.");
      num = 0;                                     
      
      for(i = 0; i < 20; i++){
      b[i+1] = strtok(NULL, " ,;.");
      num++;                                           /*Counts the number of tokenized strings*/
      if (b[i]) printf("%s\n", b[i]);
        else break;
        }
    
      num = num - 1;
      printf("\n%d\n", num);
      
    char temp[20]; //MAX = largest element of data in array
      
      for(i = 0; i < num; i++) {
        for(j = i+1; j < num; j++) {
          if((strcmp(b[i], b[i+1])) > 0) {  //str1 > str2, so we need to swap
            strcpy(temp, b[i]);
            strcpy(b[i], b[i+1]);
            strcpy(b[i+1], temp);
          }
        }
      }
      
    for(i = 0; i < num; i++){
    printf("%s\n", b[i]);
    }

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your code is for bubble sort, and this is selection sort. Bubble sort compares only adjacent values, selection sort compares i and j indexed items, instead of i and i + 1. Bubble code might work here, but I've never tried it with this sort.

    I didn't mention that, did I? Oops!

    Quote Originally Posted by patso View Post
    as soon as I go over three words, it won't alphabetize them....

    Code:
      printf("Please enter the string to be parsed:\n");   
      gets(buffer5);
      printf("\n");
        
      char *b[20];
    
    
      b[0] = strtok(buffer5, " ,;.");
      num = 0;                                     
      
      for(i = 0; i < 20; i++){
      b[i+1] = strtok(NULL, " ,;.");
      num++;     /*Counts the number of tokenized strings*/
      if (b[i]) printf("%s\n", b[i]);
        else break;
        }
    
      num = num - 1;
      printf("\n%d\n", num);
      
    char temp[20]; //should be at or very near the top of the function
    
    /* do you have good data at this moment? Let's see: */
    
    printf("\n\n Displaying contents of *b[]");
    for(i = 0; i < num; i++){
    printf("%s\n", b[i]);
    
    printf("\n press enter to continue");
    getchar();
      
      for(i = 0; i < num; i++) {
        for(j = i+1; j < num; j++) {
          if((strcmp(b[i], b[j])) > 0) {
            strcpy(temp, b[i]);
            strcpy(b[i], b[j]);
            strcpy(b[j], temp);
          }
        }
      }
     
    for(i = 0; i < num; i++){
    printf("%s\n", b[i]);
    }
    Try that. If you have memory issues, it will still crash after a few names.
    Last edited by Adak; 02-25-2010 at 11:34 AM.

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    i don't know what you mean. I thought i was using bubble sort

  13. #28
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    this is the output
    print a line:asdf

    This is a loop
    This is the second buffer
    asdf
    The line to print:
    asdf
    The new line is:
    fdsa
    Please enter the string to be parsed:
    this,is.a.string.to ;;;;parse

    this
    is
    a
    string
    to
    parse

    6


    Displaying contents of *b[]this
    is
    a
    string
    to
    parse

    press enter to continue
    is
    is
    parse
    rse
    s
    to

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Selection sort is bubble sort with a slight optimization. It doesn't compare adjacent values except the first time it enters the inner for loop. It is in the same "family" of sorters as bubble sort. Most people would call selection sort, bubble sort, since the bubble sort is has all the buzz.

    So, no memory problems?

  15. #30
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    works great for single inputted letters, as soon as there is more it gets screwed up

Page 2 of 3 FirstFirst 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