Hello all, I have a question on how to use strtok funtion for the below scenario.

A1.1*7.1&9.1&11.1/

I want to parse through & and make it as null (before 9.1) and once after I should pick the start end range between *. Say here 1.1 is start range and end range is 7.1. Once I found this I have to go to next & and take the value 9.1 and after 11.1

I thought this is simple, but end up in lossing the pointer. Please help.

Just with strtok would be problematic. However, you can use strtok to split the string on & like this:

A1.1*7.1
9.1
11.1/

Then manually parse each token to get the ranges. For example:

char s[] = "A1.1*7.1&9.1&11.1/";
char *tok = strtok(s, "&");

while (tok != NULL) {
    char *sep = strchr(tok, '*');

    if (sep != NULL) {
        // Found a range
        printf("%.*s - %s\n", sep - tok, tok, sep + 1);
    }
    else {
        // No range
        printf("%s\n", tok);
    }

    tok = strtok(NULL, "&");
}

Edited 2 Years Ago by deceptikon

During 16th like the tok address becomes zero, becuase it had two nulls in between

This is the code which is not working for me:

tempTicketingNameSelectRawData points to below
1.1-3.1&6.1&7.1/

char * delimiters = "&/";
char * tempNameNumber = // First Strtok and then loop until NULL returned
strtok(tempTicketingNameSelectRawData,delimiters);

           if(strstr(tempNameNumber,"-"))                     // Name.nbr range specified, normalize every
           {                                                  // name.nbr in the range specified
              strcpy(startingRange,                           // Extract the start Pax name.number
                       strtok(tempNameNumber, "-"));
              strcpy(endingRange,                             // Extract the ending Pax name.number
                       strtok(NULL,"-"));

          }

        tempNameNumber=strtok(NULL, delimiters);             --> here tempnamenumber becomes zero address

Edited 2 Years Ago by satys.sara: - instead of *

This is the code which is not working for me:

tempTicketingNameSelectRawData points to below
1.1-3.1&6.1&7.1/

char * delimiters = "&/";
char * tempNameNumber = // First Strtok and then loop until NULL returned
strtok(tempTicketingNameSelectRawData,delimiters);

           if(strstr(tempNameNumber,"-"))                     // Name.nbr range specified, normalize every
           {                                                  // name.nbr in the range specified
              strcpy(startingRange,                           // Extract the start Pax name.number
                       strtok(tempNameNumber, "-"));
              strcpy(endingRange,                             // Extract the ending Pax name.number
                       strtok(NULL,"-"));

          }

        tempNameNumber=strtok(NULL, delimiters);             --> here tempnamenumber becomes zero address

Edited 2 Years Ago by satys.sara: comment

Do you have to use strtok() for that? You could just parse the string with a pointer, copying each field into another char array.

i used FOR loop and took the value before and after "-" and thought this forum will be helpfull and not that much i expected. sorry.

Is this program for school or for a job? You didn't answer my last question. Here is one way to do it. extract() retrieves all the digits from the string returned by strtok(), and stops converting when it encounters anything else but a digit or '.'. You could easily modify this to store the data in whatever arrays you want to produce start and end values you need.

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>


char* extract(char* p)
{
    int i = 0;
    char buf[40] = { 0 };
    while (*p && (isdigit(*p) || *p == '.') )
    {
        buf[i++] = *p;
        ++p;
    }
    buf[i] = '\0';
    printf("%s\n", buf);
    return p;

}

int main()
{
    char str [] = "A1.1*7.1&9.1&11.1/";
    char *ptr = strtok(str, "&");
    while (ptr != NULL)
    {
        char* p = ptr;
        while (!isdigit(*p))
            ++p;

        p = extract(p);
        if (*p == '*')
            p = extract(++p);
        ptr = strtok(NULL, "&");
    }


}
This article has been dead for over six months. Start a new discussion instead.