Solved

strtok failing when word is empty in one of delemter

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

Suggested Solutions

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…
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

740 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