satya
satya

Reputation: 109

Tokenize the string in c

i need to tokenize the string in c. suppose if i have a string like this "product=c,author=dennis,category=programming".

I want to extract only the values among these key values pairs. Like

[c,dennis,programming].

I have used strtok function which tokenizes with "=" and I get values

[product,c,author,dennis,category,programming].

Is there any built in function that can generate only the values like mentioned above.

Upvotes: 1

Views: 1465

Answers (4)

John Bode
John Bode

Reputation: 123458

You can first tokenize on ,, splitting the contents into 3 different strings, then tokenize on '=' for each of those strings:

char *kvpair[N] = {NULL}; // where N is large enough for the expected
                          // number of key-value pairs 
char *tok = strtok(input, ",");
size_t kvcount = 0;
while (tok != NULL && kvcount < N)
{
  kvpair[kvcount++] = tok;
  tok = strtok(NULL, ",");
}
...
for (i = 0; i < kvcount; i++)
{
  char delim = '[';
  char *key = strtok(kvpair[i], "=");
  char *val = strtok(NULL, "=");
  printf("%c%s", delim, val);
  delim = ',';
}
putchar(']');

This is just a rough sketch; it assumes that the maximum number of key-value pairs is known ahead of time, it doesn't attempt to handle empty keys or values, or really do any sort of error handling at all. But it should point you in the right direction.

Remember that strok modifies its input; if your original data is a string literal or if you need to preserve the original data, you'll need to make a copy and work on that copy.

Note that, because of how strok works, you can't "nest" calls; that is, you can't tokenize the first key-value pair, then split it into key and value tokens, then get the next key-value pair. You'll have to tokenize all the key-value pairs first, then process each one in turn.

Upvotes: 0

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

Just a simple scanf

#include<stdio.h>

int main()
{
    char token[20] = { 0 };
    char c, name[20];
    int i=0, offset;
    while (scanf("%[a-z]%*[^a-z]", token) == 1) {
        i++;
        if(i%2==0)
            printf("[%s]\n",token );
    }
    return 0;
}



./a.out 
product=c,author=dennis,category=programming,
[c]
[dennis]
[programming]
Ctrl+D

Note. I have added , at the end of the string

Upvotes: 4

Gandaro
Gandaro

Reputation: 3443

You could simply skip every second token like that:

#include <stdio.h>
#include <string.h>

int main(void) {
    char str[] = "product=c,author=dennis,category=programming";
    char* p = strtok(str, ",=");

    while (p != NULL) {
        p = strtok(NULL, ",=");
        if (p != NULL) {
            printf("%s\n", p);
            strtok(NULL, ",="); // skip this
        }
    }

    return 0;
}

Upvotes: 3

Sjoerd
Sjoerd

Reputation: 75599

I can think of a couple of ways:

  • First tokenize on ,, then split each part on the =.
  • Find the first =, then the , after it, and get the word in between. Repeat.
  • If there are always three values, you can use sscanf to read the values.
  • You can use a regex library to parse the string.

Upvotes: 1

Related Questions