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;
}
char *rudi = strstr(sentence, "rudolph=")and take it from there. But you say you want to extract "rudolf" too, so it is unclear whether the sentence is known to contain that keyword, or perhaps some other, such as "reindeer=42",strcpy(name, "rudolf"); value=12;and ignore the long string andscanfaltogether. If you don't know that the input string contains "rudolf" and "12", please explain what you do know, and what you need to extract in terms of what you know.'='character withchar *eptr = strchr(sentence, '=');then work left to find the keyword and right for its value. If the sentence may contain more than one such, continue the search from there.