2021-07-18

How to extract some formatted string from the buffer using scanf?

I need to extract both "rudolf" and "12" from that long string: "hello, i know that rudolph=12 but it so small..." using scanf, how can I do it?

This buffer can contains any formatted strings like ruby=45 or bomb=1, and I dont know it in advance.

I am trying something like that, but it was unsuccessful

#include <stdio.h>

int main()
{
    char sentence[] = "hello, i know that rudolph=12 but it so small...";
    char name[32];
    int value;

    sscanf(sentence, "%[a-z]=%d", name, &value);
    printf("%s -> %d\n", name, value);

    getchar();
    return 0;
}


from Recent Questions - Stack Overflow https://ift.tt/3BeBIt6
https://ift.tt/eA8V8J

No comments:

Post a Comment