I am working on a project where I am using the "strtok" function to set the tokens in a line of text and then output tokens in reverse order.... Per example below i need the output to revers line of text: "hello world" -> "dlrow olleh" Not sure how to get this 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"; /* For a line length equal to 29, there can be no more than something like 15 tokens, but I'll just use 30 so that I won't have worry about the details. Bottom line: make sure the array of pointers is large enough to hold addresses of all of the tokens. */ 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 The proble is below... I now the code to print out the tokens in reverse order.... [COLOR="Red"][B] NEED SOME IN PUT ON IDEAS TO GET IT TO WORK[/B][/COLOR] */ printf("\n===========================\n"); printf("===========================\n\n"); printf("The line of text in revers:\n\n"); while ( token_ptr != NULL ) { for ( i = -1; i >= 0; i--) { /*line += *tokens[i];*/ printf("%s ", tokens[i]); } } /*printf("%s ", tokens[i]);*/ getch(); return 0; }
first issue while ( token_ptr != NULL ) { for ( i = -1; i >= 0; i--) { /*line += *tokens;*/ printf("%s ", tokens); } } The entire section is problematic. 1. while condition is always false; because (take a closer look to the last for loop) you are getting out of the for loop while token_ptr is getting null. 2. secondly i dont know what you want.. a) to show reverse every single word in the line or b) to show reverse the entire line. for both case entire section is wrong. for case b) for(i=strlen(line)-1;i>=0;i--) printf("%c",line); printf("\n"); for case a) for(int j=i-1;j>=0;j--) { printf("%s\n",tokens[j]); } both of the solution will be replacement of while loop. second issue 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 = token_ptr; printf("tokens[%d]: <%s>\n", i, tokens); token_ptr = strtok(NULL, " "); } printf("\nNumber of tokens = %d\n\n", i); in the line 8 you have token_ptr = strtok(line, " \t\n\n"); /* any whitespace */ but in the line 14 you have done token_ptr = strtok(NULL, " "); in the line 10 you have write for (i = 0; i < max_tokens && token_ptr != NULL; i++) but the value you get max_tokens is quite interesting. lets check the line int max_tokens = sizeof(tokens)/sizeof(tokens[0]); the value of sizeof(tokens[0]) is 4. The reason is its a pointer. If i am not wrong size of any pointer will return 4. even if it is a pointer of structure. (in 32 bit processor it is 4 but in 16 bit processor it is 2. that is the length of int variable in byte) sizeof(tokens) is 60(4*15). if you had defined char *token[10]; the return of sizeof(token[0]) would be 4 and sizeof(token) would be 40(4*10). above discussion was for the for loop. in the for loop you have limited it with max_tokens value. that is if the token variable is something like char *token[5]; you would be able to store 5 token in your variable. or if your variable is something like char *token[20] you will be able to store 20 token in your variable. but char *token[15] mean you can store (2^31-1)/15 number of token and each token can be maximum 15 byte long. (if my calculation is right. 2^31-1 would work for 32 bit processor, you can define this value as 2^(sizeof(int)*8-1). this is the length of maximum number of array you can define or directly use in c compiler)(^ stands for power) (i am not that good student. but i try my best. if any info provided wrong please first accept my apology and then let me know.). best regards, johny