Mar 31, 2001 #1 donniea21 MIS Feb 16, 2001 75 US All im trying to do have a program that tokenizes a string and then prints out the tokens on seperate line and in reverse order. i.e User inputs "Hello World" Program outs: World Hello The use of strcat was suggested but i cant figure it out.
All im trying to do have a program that tokenizes a string and then prints out the tokens on seperate line and in reverse order. i.e User inputs "Hello World" Program outs: World Hello The use of strcat was suggested but i cant figure it out.
Mar 31, 2001 1 #2 rbobbitt Programmer Aug 17, 2000 566 US You already asked this question, didn't you go back to the original thread, there's answer there. One correction though to my answer is I made a mistake in the piece of code that prints out the tokens. It should be something like this instead: while (tokenCtr) { printf("%s\n",tokens[--tokenCtr]); } Russ bobbitts@hotmail.com http://home.earthlink.net/~bobbitts Upvote 0 Downvote
You already asked this question, didn't you go back to the original thread, there's answer there. One correction though to my answer is I made a mistake in the piece of code that prints out the tokens. It should be something like this instead: while (tokenCtr) { printf("%s\n",tokens[--tokenCtr]); } Russ bobbitts@hotmail.com http://home.earthlink.net/~bobbitts
Apr 3, 2001 #3 abp Programmer Sep 14, 2000 134 FR Try this, #include <stdio.h> #include <string.h> #define MAX_TOKENS 50 main() { char *str="String to be tokenized"; char *tokens[MAX_TOKENS]; char *token; int i,len; i= 0; while ( (token = strtok(str, " ") != NULL) { tokens[i++]=token; if (str) str=strchr(str, ' '); } len=i--; for (i=len;i>=0;i--) printf("%s\n", tokens); } Upvote 0 Downvote
Try this, #include <stdio.h> #include <string.h> #define MAX_TOKENS 50 main() { char *str="String to be tokenized"; char *tokens[MAX_TOKENS]; char *token; int i,len; i= 0; while ( (token = strtok(str, " ") != NULL) { tokens[i++]=token; if (str) str=strchr(str, ' '); } len=i--; for (i=len;i>=0;i--) printf("%s\n", tokens); }
Apr 4, 2001 #4 rbobbitt Programmer Aug 17, 2000 566 US Didn't try to run this by any chance? Instead of: len=i--; len=--i; OR len=i-1; The way you have it, i isn't decremented until AFTER the assignment. Russ bobbitts@hotmail.com http://home.earthlink.net/~bobbitts Upvote 0 Downvote
Didn't try to run this by any chance? Instead of: len=i--; len=--i; OR len=i-1; The way you have it, i isn't decremented until AFTER the assignment. Russ bobbitts@hotmail.com http://home.earthlink.net/~bobbitts
Apr 4, 2001 #5 sarnath Programmer Dec 8, 2000 69 IN ABP is right... strtok -> Ideal choice ! Aha ! Do not rejoice that ur code works. it might be a special case of an error :-( Upvote 0 Downvote
ABP is right... strtok -> Ideal choice ! Aha ! Do not rejoice that ur code works. it might be a special case of an error :-(
Apr 5, 2001 #6 abp Programmer Sep 14, 2000 134 FR rbobbit is right, i actually ran and tested the program on an IRIX 6.3 with cc... The code was len=i-1; When i posted, i changed it to len=i--; But of course the fact that it will assign the current value of i to len, skipped me... :-| Thanks rbobbit. abp Upvote 0 Downvote
rbobbit is right, i actually ran and tested the program on an IRIX 6.3 with cc... The code was len=i-1; When i posted, i changed it to len=i--; But of course the fact that it will assign the current value of i to len, skipped me... :-| Thanks rbobbit. abp
Apr 5, 2001 #7 sarnath Programmer Dec 8, 2000 69 IN thaz ok, ABP. That was minor Do not rejoice that ur code works. it might be a special case of an error :-( Upvote 0 Downvote
thaz ok, ABP. That was minor Do not rejoice that ur code works. it might be a special case of an error :-(