:-/AnyWay,, Here is what it is supposed to happen....

The input is a char array initialized with line of Text,, the program should tokenizes the line with the "strtok" function and should output the line of text in reverse order....

I need to set up a "strcpy"..... I'm almost there just cant seem
to get this to work...

#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, " " );


              /* NOT SURE HOW TO IMPLAMETN */
              /* ?????????????????????????????? */
	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;
}

>strcpy( *revrseStrng, string );
I don't know what you imagined this to do, but there's no magic in C. If you want to build a string from the tokens of another, you do it just as one would expect:

while more tokens
  get token
  append token to string

print string

Likewise, to build the same string in reverse you prepend instead of append. However, prepending in C is somewhat awkward, so I'll suggest an algorithm that's a bit more elegant:

while more tokens
  get token
  reverse token
  append token to string

reverse string
print string

Here's how it works with a source of "This is a test":

Append reversed tokens:
dest: "sihT"
dest: "sihT si"
dest: "sihT si a"
dest: "sihT si a tset";

Reverse dest:
"test a is This"

See if you can implement that on your own.

Well almost gooter done,,,, just need to run through the token array in reverse order to get out put to look like example...

Per your examples this is what the output should be:

"hello world" -> "dlrow olleh"

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 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;
}

>Per your examples this is what the output should be:
>"hello world" -> "dlrow olleh"

Um, no? Watch again:

1) Reverse and add "hello":
"olleh"

2) Reverse and add "world":
"olleh dlrow"

3) Reverse the whole thing:
"world hello"

>Per your examples this is what the output should be:
>"hello world" -> "dlrow olleh"

Um, no? Watch again:

1) Reverse and add "hello":
"olleh"

2) Reverse and add "world":
"olleh dlrow"

3) Reverse the whole thing:
"world hello"

I certainly appreciate you taking your time to run over the flow
of the program to me,,,, it is a kind gester...

I do get the just of what needs to happen with the "printf"... I'm just hoping to find someone that can help me out with the actual coding below...

As I stated,,, I am greateful for any help... Just not sure how to set up printing the "token" array in reverse..... ANY IDEAS!!!!

/* 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]);		}	}

*sigh* Sometimes I wonder why I even bother when nobody listens:

while ( --i >= 0 )
  puts ( tokens[i] );
Comments
I wouldn't be surprise if he has deterred beginning programmers from programming
That ain't true. ;)
This article has been dead for over six months. Start a new discussion instead.