Solved

strtok failing when word is empty in one of delemter

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

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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 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 switch statements in the C programming language.

771 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

10 Experts available now in Live!

Get 1:1 Help Now