Solved

strtok question

Posted on 2006-07-07
12
387 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

810 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