Try the following approach
#include <stdio.h>
#include <string.h>
int main(void)
{
enum Animal { cat, dog, elephant };
char * animal_name[] = { "cat", "dog", "elephant" };
enum Animal b;
size_t n;
char s[100];
fgets( s , sizeof( s ), stdin );
n = strlen( s );
if ( s[n - 1] == '\n' ) s[n - 1] = '\0';
b = dog;
if ( strcmp( animal_name[b], s ) == 0 ) printf( "Y\n" );
else printf( "N\n" );
return 0;
}
If you enter dog, the output will be
Y
Also, before comparing strings, you can convert the entered string to lowercase, which is used in the strings of the array of animal names.
Or you can use C # instead of C, where enumerations can be converted to strings. :)
source
share