parsing Two-dimensional array in c
Posted
by gitter78
on Stack Overflow
See other posts from Stack Overflow
or by gitter78
Published on 2010-05-01T19:25:49Z
Indexed on
2010/05/01
19:47 UTC
Read the original article
Hit count: 194
I'm trying to parse an array that looks like the one below:
char *arr[][2] = {
{ "1", "Purple" },
{ "2", "Blue" },
{ "22", "Red" },
...
};
I was thinking having a loop as:
char *func(char *a){
for(i = 0; i<sizeof(arr)/sizeof(arr[0]);i++){
if(strstr(a,arr[i][0])!=NULL) return arr[i][1];
}
}
char *out;
const char *hello = "this is my 2 string";
out = func(hello);
In this case, I'm trying to get the second value based on the first one: Purple, Blue Red, etc..
The question is how would go in parsing this and instead of printing out the value, return the value.
UPDATE/FIXED: It has been fixed above.
Thanks
© Stack Overflow or respective owner