• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

strtok question

void getData()
{
        char st[256];
        char s[256];
        char *tokenPtr;
        int i=0;
      printf("Enter String: ");
        scanf("%s", st);
        tokenPtr=strtok(st, " ");
      while(tokenPtr != NULL) {
            sprintf(s, "%s", tokenPtr);
            tokenPtr = strtok(NULL, " ");
      }
      strcpy(s, "\0");
        printf("STRING=%s\n", s);
 }

In the above code, strtok is not able to get two or three words seperated by a blank. It is able to get only one word. Could somebody explain why?
If you print the string s it will print only one word not multiple words.

0
sudarshantk
Asked:
sudarshantk
  • 5
  • 4
  • 3
1 Solution
 
jkrCommented:
The culprit is

     strcpy(s, "\0");

which effectively voids the tokens thast are found. You don't need that statement, since the tokens are already zero-terminated, just leave it out.

0
 
sudarshantkAuthor Commented:
sorry it should be strcat(s, "\0")....it still prints one word instead of multiple words
0
 
jkrCommented:
Antoher problem: 'scanf()' will only read until the 1st blank, so if you want to read more - i.e. a whole line of input -  use 'gets()', e.g.

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

void getData()
{
        char st[256];
        char s[256];
        char *tokenPtr;
        int i=0;
     printf("Enter String: ");
        gets(st);
        tokenPtr=strtok(st, " ");
     while(tokenPtr != NULL) {
          printf("Token: %s\n", tokenPtr);
          sprintf(s, "%s", tokenPtr);
          tokenPtr = strtok(NULL, " ");
     }
        printf("STRING=%s\n", s);
}

int main ()
{
    getData();

    return 0;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sudarshantkAuthor Commented:
Hello r u there
0
 
jkrCommented:
Yes. As I posted above, use 'gets()' instead of 'scanf()'. The above code works.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi All,

strtok() is destructive to the original string.  It writes a string-terminator character over the separator and returns the address of the next character.

In your code above, s[] will always contain something close to the original string.  strtok() will insert '\0' bytes every time it encounters a separator.

What you want to print is tokenPtr.  It is the address of the returned string.



Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:


And the last printf() is printing the original string, terminated after the first token because strtok() put a '\0' there.


Kent
0
 
jkrCommented:
That's pretty futile if only the 1st token is stored in the 1st place ;o)
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Yeah.  :~}

It's been a long week......
0
 
sudarshantkAuthor Commented:
In the end i want to concatenate the two string...
void getData()
{
          char st[256];
          char s[256];
          char s0[256];
          char *tokenPtr;
          int i=0;
          printf("Enter String: ");
          gets(st);
          tokenPtr=strtok(st, " ");
          while(tokenPtr != NULL) {
          printf("Token: %s\n", tokenPtr);
          sprintf(s, "%s", tokenPtr);
          strcat(s0, s);
          tokenPtr = strtok(NULL, " ");
     }
        printf("STRING=%s\n", s0);
}

i put a strcat in the while loop....could you please tell me what went wrong...
0
 
Kent OlsenData Warehouse Architect / DBACommented:

You should initialize s0 to be an empty string.

You'll also probaby want to put a comma or space between the items as you strcat() them into s0.


Kent
0
 
jkrCommented:
The easiest way would be to use

          char st[256] = "\0";
          char s[256] = "\0";
          char s0[256] = "\0";

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now