Solved

strtok failing when word is empty in one of delemter

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is my core dump file in Ubuntu? 12 454
libcurl - taking long time on curl_easy_perform 11 434
Trouble linking program with -lcrypt 3 155
C#, VS15, StructLayout 1 135
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

861 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