Solved

strtok question

Posted on 2006-07-07
12
385 Views
Last Modified: 2010-04-15
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
Comment
Question by:sudarshantk
  • 5
  • 4
  • 3
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17061193
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
 

Author Comment

by:sudarshantk
ID: 17061259
sorry it should be strcat(s, "\0")....it still prints one word instead of multiple words
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 17061278
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
 

Author Comment

by:sudarshantk
ID: 17061312
Hello r u there
0
 
LVL 86

Expert Comment

by:jkr
ID: 17061328
Yes. As I posted above, use 'gets()' instead of 'scanf()'. The above code works.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 17061588
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

by:Kdo
ID: 17061600


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


Kent
0
 
LVL 86

Expert Comment

by:jkr
ID: 17061626
That's pretty futile if only the 1st token is stored in the 1st place ;o)
0
 
LVL 45

Expert Comment

by:Kdo
ID: 17061645

Yeah.  :~}

It's been a long week......
0
 

Author Comment

by:sudarshantk
ID: 17061703
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
 
LVL 45

Expert Comment

by:Kdo
ID: 17061748

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
 
LVL 86

Expert Comment

by:jkr
ID: 17061839
The easiest way would be to use

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

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now