[Solved] Enum and strings


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 to enter dog then the output is

Y

Also before comparing strings you could convert the entered string to the lower case that is used in strings of the array of animal names.

Or you could use C# instead of C where enumerators can be converted to strings. 🙂

0

solved Enum and strings