Char array: pointer and loop
#include <stdio.h>
#include <string.h>
char *p[][2] = {
"1", "red",
"2", "yellow",
"3", "red",
"4", "green",
"5", "yellow",
"6", "red",
"7", "red",
"", "" /* terminate the table with null strings */
};
int main(void)
{
int i;
char value[80];
printf("Enter value: ");
gets(value);
for(i = 0; *p[ i ][ 0 ]; i++) {
if(!strcmp(value, p[ i ][ 0 ]))
printf("%s is %s\n", value, p[ i ][ 1 ]);
}
return 0;
}
Related examples in the same category