Solved

strtok failing when word is empty in one of delemter

Posted on 2014-03-24
2
366 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
[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
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

Independent Software Vendors: 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

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…
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 writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

763 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