check if a character in string is Ñ

Viewed 78

There is a way to do something like this?

    char foo[] = "HOLA Ñ MUNDO";
    int coord = -1, i = 5;
    printf("%c\n",foo[i]); //Always return "�"
    if (foo[i] == 'Ñ')
        coord = 14; //It's like A is the first letter of alphabet, B is the second one, and so on...
    printf("coord:%d\n",coord); //Always return -1, so, foo[i] != 'Ñ' (but it is...)

I tried using #include <locale.h> and calling the function setlocale(LC_ALL, "spanish"); but it doesn't work.

1 Answers

Solved, thanks to the comments from @Carl Norum and @Jeff Holt

    #include <locale.h>

    ...

    char foo[] = "HOLA Ñ MUNDO";
    int coord = -1, i = 5;
    printf("%c\n",foo[i]); //Always return "�"
    setlocale(LC_CTYPE, "en_US.utf8");
    int char_len = mblen(&foo[i], MB_CUR_MAX);
    if (memcmp(&foo[i], "Ñ", char_len) == 0) //if (foo[i] == 'Ñ')
        coord = 14; //It's like A is the first letter of alphabet, B is the second one, and so on...
    printf("coord:%d\n",coord); //Now it return 14
Related