arrays - C language strtok() specific element after delimiter -
i have series of strings each letters, whitespace, numbers , $. can't seem figure out how number(s) following $? example,
char s[50]= "i have $20 , 3 hours left work."; char t[50]= "he might have 4 left have $7."; char u[50]= "$29 , 30 equal 59 dollars."; /* i'm assuming strtok, since makes sense me */
expected output
20 7 29
any appreciated!
#include <stdio.h> #include <string.h> void print(int val){ printf("%d\n", val); } void get(const char *s, void (*callback)(int)){ int value; while((s = strchr(s, '$')) != null){ if(1==sscanf(++s, "%d", &value)){ callback(value); } } } int main(void){ char s[50]= "i have $20 , 3 hours left work."; char t[50]= "he might have 4 left have $7."; char u[50]= "$29 , 30 equal 59 dollars."; char *a[] = { s, t, u}; for(int = 0; < sizeof(a)/sizeof(*a); ++i){ get(a[i], print); } return 0; }
Comments
Post a Comment