Solved

strtok question

Posted on 2006-07-07
12
386 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

867 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

18 Experts available now in Live!

Get 1:1 Help Now