Convert a spanish string to uppercase

Viewed 104

I want to convert a Spanish string to uppercase. I use the function toupper for all characters. The function does not convert characters with accent:

PROTECCIóN

Nor the enye character:

DAñO

I use the setlocale function.

setlocale(LC_ALL, "es_ES");
1 Answers

It is possible to get the good result with setlocale(LC_ALL, "es_ES.utf8");, and use of wchar_t and of towupper(.) function.

Output

protección daño  PROTECCIÓN DAÑO

#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>

int main(void) {
    if (!setlocale(LC_ALL, "es_ES.utf8")) return 1;
    wchar_t input[] = L"protección daño";
    int length = sizeof(input)/sizeof(input[0]);
    wchar_t output[length];
    for (int i = 0; i < length-1; ++i) {
        output[i] = towupper(input[i]);
    }
    output[length-1] = '\0';
    printf ("%ls  %ls\n", input, output);
    
    return 0;
}
Related