Solved

strtok question

Posted on 2006-07-07
12
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 

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:Kent Olsen
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:Kent Olsen
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:Kent Olsen
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:Kent Olsen
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to set environment variables in C 2 103
Global Keyboard Hooks Blocked 4 99
How to translate this 2-line while loop into C from Perl? 8 165
Why isn't object file created? 6 106
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

734 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