Tokenize a line with strtok & reverse line out..

This is a discussion on Tokenize a line with strtok & reverse line out.. within the C Programming forums, part of the General Programming Boards category; Any Help would be appreciated... I have been taking the time to search the WWW for examples... And have found ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Location
    Aurora, CO
    Posts
    9

    Angry Tokenize a line with strtok & reverse line out..

    Any Help would be appreciated... I have been taking the time to search the WWW for examples... And have found several good once but there not useing TOKEN....


    AnyWay,, Here is what it is supposed to happen....

    The user inputs a line of text,, tokenizes the line with the "strtok" function and should output the line of text in reverse order....

    Cant seem to get passed some error on the back end...


    Code:
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char string[100];
    	char *tokenPtr;
    	char *reverse[100];
    	int i;	/* counter */
    
    	
    	printf( "Enter a line of text; ");
    	scanf(string);
    	printf( "\n" );
    
    
    	tokenPtr = strtok(string, " " );
    	
    
    
    	while(tokenPtr != NULL)
    	{	
    		for ( i = 0; i < 100; i++)
    		{
    			reverse [i] = tokenPtr;
    		}
    
    		tokenPtr = strtok(NULL, " " );	/* get next token */
    
    	}
    
    		for ( i = 100; i > 0; i--)
    		{
    			printf("%s ", reverse[i]);
    		}
    
    	
    
    	getch();
    
    	return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Reverse order, meaning what?

    "hello world" = "world" "hello"
    "hello world" = "dlrow olleh"

    Let's assume the former. It's easy with recursion. If you don't want to use recursion, then you need to keep a list of all the segments that you're getting (how about an array of pointers to characters).

    Looking at your above code, your last loop shouldn't start at 100. It should start at whatever it was at the end of the first loop.


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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Location
    Aurora, CO
    Posts
    9

    Talking "how about an array of pointers to characters"

    I need to set up a "strcpy"..... I'm almost there just cant seem
    to get this to work... Also you stated "I needed more space"...
    I believe you speaking to my array size... but not sure...

    AnyOne got any more suggestions... I'm still working it....



    Code:
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char string[30] = "I need to see this in reverse"; /* initialize char array */
        char *revrseStrng[30];        /* create char array revrseStrng */
        char *tokenPtr;
        int i;    /* counter */
    
    
           /* copy contents of string into revrseStrn 
            printf( "%s%s\n%s%s\n", 
            "The string in array x is: ", string,
            "The string in array y is: ", strcpy( revrseStrng, string ) ); 
            */
    
    
        
    
        tokenPtr = strtok(string, " " );
    
        strcpy( *revrseStrng, string );
        
    
    
        while(tokenPtr != NULL)
        {    
            for ( i = 0; i < 30; i++)
            {
                revrseStrng[i] = tokenPtr;
            }
    
            tokenPtr = strtok(NULL, " " );    /* get next token */
    
        }
    
            for ( i = 30; i > 0; i--)
            {
                printf("%s ", revrseStrng[i]);
            }
    
        
    
        getch();
    
        return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MAV_DevWantB View Post
    Also you stated "I needed more space"...
    I believe you speaking to my array size... but not sure...
    Considering that nowhere in my post do I mention anything about space, I too am "not sure". In this case, I'm not sure who you think you're replying to.


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

  5. #5
    Registered User
    Join Date
    Aug 2009
    Location
    Aurora, CO
    Posts
    9

    Following Considered..... So Sorry,,,,,

    Mr. quzah,,,

    Please except my apology with the last thread... I am new to these forums and am trying my best to hang out with the big dogs and hoping not to step into my dung...

    I have been attempting to understand how to use strtok successfully,, I do get the just of it... but just having problems...

    I honestly thought that "(how about an array of pointers to characters)." was a good idea for the strcpy... Im still fuzzy will keep reading and searching WWW...


    thnks,

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you already have the array of character pointers, so you're on the right track. You do need to be allocating space though for each token chunk. Something like:
    Code:
    x = 0; /* spot to start in your array */
    p = strtok( this, " " ); /* your first strtok */
    do
    {
        if( p )
        {
            words[ x ] = malloc( strlen( p ) + 1 );
            strcpy( words[ x ], p );
            x++;
            p = strtok( NULL, " " );
        }
    }
    while( p && x < SOMESIZE );
    Something similar should work.


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

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by MAV_DevWantB View Post
    Mr. quzah,,,

    Please except my apology with the last thread... I am new to these forums and am trying my best to hang out with the big dogs and hoping not to step into my dung...
    But you still haven't answered quzah's question about what you mean be reverse line out, as in

    "hello world" -> "world hello" , or
    "hello world" -> "olleh dlrow" , or
    "hello world" -> "dlrow olleh"

    Quote Originally Posted by MAV_DevWantB View Post
    I have been attempting to understand how to use strtok successfully,, I do get the just of it... but just having problems...

    I honestly thought that "(how about an array of pointers to characters)." was a good idea for the strcpy... Im still fuzzy will keep reading and searching WWW...


    thnks,
    You would have had the solution by now if you had given a straightforward reply to quzah instead of goin' around in circles.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Location
    Aurora, CO
    Posts
    9

    Talking Regarding patter on "revers line text out"....

    Thnks itcbitc you are correct I have not confirmed the requested output on my line of text....

    Per your examples this is what the output should be:

    "hello world" -> "dlrow olleh"


    Sorry to not comfirm,,,, hope to be more clear on future postings as I can see I will need more exposure to the forums to truly evolve with these projects...

    I have gotten a bit further with the help of some kind souls on the WWW... So far all that is left is to print out the token[i] in reverse order....

    Not sure why it is not working... Any input would be appreciated.. I think the token seems to be throwing me off..........

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char line[30] = "I need to see this in reverse"; 
        char *tokens[15];
        int max_tokens = sizeof(tokens)/sizeof(tokens[0]);
        char *token_ptr;
        int i;
    
    
        printf("Original line of text: <%s>\n\n", line);
    
        token_ptr = strtok(line, " \t\n\n");	/* any whitespace */
    
        for (i = 0; i < max_tokens && token_ptr != NULL; i++) 
    	{
            tokens[i] = token_ptr;
            printf("tokens[%d]: <%s>\n", i, tokens[i]);
            token_ptr = strtok(NULL, " ");
        }
    
        printf("\nNumber of tokens = %d\n\n", i);
    
    		/* Now just print out tokens[i] starting with 
    		index equal to i-1 and going down to zero */
    
    	printf("\n===========================\n");
    	printf("===========================\n\n");
    	printf("The line of text in revers:\n\n");
    
    
    	/* ???????????????????????????????????????????? */
    	/* ???????????????????????????????????????????? */
    
    	/* CANT SEEM TO GET THE LOOP BELOW TO WORK */
    
    
     
                   while ( token_ptr != NULL )
    	{
    		for ( i = -1; i >= 0; i--)
    		{
    			/*line += *tokens[i];*/
    
    			printf("%s ", tokens[i]);
    		}
    	}
    
    			/*printf("%s ", tokens[i]);*/
    
    
    
    	getch();
    
    	return 0;
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If your token is whitespace, all you are doing is chopping 'words' up. You would need to call a function to reverse the contents of each word, and then to print those words in the reverse order you got them.
    Code:
    while( ... )
        get a word
        reverse that word
        count++
    
    for count, count > -1; count--
        print word
        print space
    But really, if all you are doing is printing the entire thing in the reverse order you get it, I'm not sure why you need strtok, other than just as a learning exercise. It seems a poor choice for this sort of task.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  2. How to I/O a file line by line??
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 03-13-2002, 05:22 PM
  3. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM

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