Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TOKENIZE PROBLEM... 1

Status
Not open for further replies.

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.
 
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
 

Try this,

#include <stdio.h>
#include <string.h>

#define MAX_TOKENS 50
main()
{

char *str=&quot;String to be tokenized&quot;;
char *tokens[MAX_TOKENS];

char *token;

int i,len;

i= 0;
while ( (token = strtok(str, &quot; &quot;)) != NULL)
{

tokens[i++]=token;
if (str)
str=strchr(str, ' ');
}

len=i--;

for (i=len;i>=0;i--)
printf(&quot;%s\n&quot;, tokens);
}

:)
 
ABP is right...

strtok -> Ideal choice ! Aha !
Do not rejoice that ur code works.
it might be a special case of an error :-(
 
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 :)
 

thaz ok, ABP. That was minor
Do not rejoice that ur code works.
it might be a special case of an error :-(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top