Solved

strtok failing when word is empty in one of delemter

Posted on 2014-03-24
2
363 Views
Last Modified: 2014-03-25
char myWords[100] = "asdlakm#asdasd##asdas#sas";
char *temp;
   
   temp = strtok(myWords, "#");
    printf("1. %s\n", temp);
    int i = 1;
    while(temp)
   {
       
    temp = strtok(NULL, "#");
    i++;
    printf("%d. %s\n", i, temp);
   }

out put:
1. asdlakm
2. asdasd
3. asdas
4. sas
5. (null)

Expected out put:
1. asdlakm
2. asdasd
3.
4. asdas
5. sas

Please let me know how to handle this type of issue. Here strtok is ignoring the # and returning the next work.
0
Comment
Question by:priyanka999kamlekar
2 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 39949975
Hi priyanka999kamlekar,

the strtok isn't intended to work the way you expect. It's functionality is to find tokens speparated by one or more characters of a set of delimiters, it's not intended to find the delimeters itself. So in your case there's no difference between "A#B" and "A###B" since it only contains two tokens. In fact it's good strtok works this way because this way you can i.e. skip multiple whitespaces by using delimiter like " \t".

IMO you should try to implement a loop to search for all '#' i.e. using strstr function instead.

ZOPPO
0
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 39950051
char myWords[100] = "asdlakm#asdasd##asdas#sas";
char *temp;
char *s=myWords;

   temp = strsep(&s, "#");
    printf("1. %s\n", temp);
    int i = 1;
    while(temp)
   {

    temp = strsep(&s, "#");
    i++;
    printf("%d. %s\n", i, temp);
   }
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 create, access, and change arrays 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.

813 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

11 Experts available now in Live!

Get 1:1 Help Now