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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PLEASE HEELLLLPPP!!! 2

Status
Not open for further replies.

Adia

ISP
May 18, 2001
1
US
I have a program that tokenize a string line input by user.
I need to know how to print tokens in reverse order.
Thank you so much
 
Use the following program.

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

int main( void )
{
char *str, *tok, *tokArray[100];
int i, n;
str = (char *) malloc(sizeof(char) * 100);
gets(str);
printf( &quot;%s\n&quot;, str);
tok = strtok(str, &quot; &quot;);
for(i = 0; tok != NULL; i++)
{
tokArray = (char *) malloc( strlen(tok) + 1);
strcpy(tokArray, tok);
tok = strtok(NULL, &quot; &quot;);
}
for(i = i - 1; i >= 0; i--)
printf(&quot;.......%s\n&quot;, tokArray);
return 0;
}

otherwise check for the whitespace characters in the input array from end.

Regards
Maniraja S
 
Hi ManiRaja,
Pls check that u try not to use gets() coz it does not keep a check on no. of chars entered by user instead try using fgets(). And also remember to uncheck the &quot;Proces TGML&quot; check box below the textbox where u type in reply.

Regards,
SwapSawe.
 
Also, you don't need to use the malloc() call each time, rather just have the array member point to tok each time:


for(i = 0; tok != NULL; i++)
{
tokArray=tok;
tok = strtok(NULL, &quot; &quot;);
}

You'll also want to check the value of i each time to make sure that you don't exceed your array bounds.



Russ
bobbitts@hotmail.com
 
Hi rbobbitt,

The string in the tok is changing each time of the loop iteration. So just storing the address will give only the last string in the tok to all the elements in the tokArray at the end of the program.

Regards
Maniraja S
 
Yes its working, but it will give problems.

In the man page of the strtok states that the strok function will change the orginal string[ str ], based on this I thought strtok function will return the parsed string from a new memory location and that memory will be valid upto the next call of strtok but its not.

Actually they are just replacing the delimiter character in the original string with \0 and returning the next characters address.

If we remove the original string [ ie str in this case ], the tokArray will not give the correct value because the memory area pointed by tokArray are removed, will have garbage values only and cause segmentation fault. In this case we have to allocate memory and copy the resultant string to it.

If we are not removing the original string its ok.

Regards
Maniraja S
 
Indeed, good point. In non-trivial cases, you should probably use some method that ensures the resulting tokens are not pointers into the original string, such as the way smaniraja does it above.

Only if you *know* that the original string will not be changed and will not go out of scope during the time that you need the tokens (which is the case in the simple program above) should you rely on the method I posted above.

Russ
bobbitts@hotmail.com
 
Neat code - can anyone do the same without using the strtok function?
 
Try this (no constraints except stack size;):
Code:
static const char seps[] = " \t\r\n";

const char* Tok(const char* str)
{
  int n;

  if (str && *str)
  {
     str += strspn(str,seps);
     n = strcspn(str,seps);
     if (n)
     {
        Tok(str+n);
        while (n--)
           putchar(*str++);
        putchar('\n');
     }
   }
return str;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top