简体   繁体   中英

Ansi C - Convert String to Lower Case String

I need to convert a char array to a lower case char array. I also want to convert some special characters like Ä to ä. But the "ASCII-Code" 196 isn't in the ASCII-128. How can I convert them to lower case ? I can use them as string initializer but can't really deal with their codes. In case this might be a compiler option I'm using eclipse CDT on Linux without c99 mode (can't use that one).

char* toLowerCase(char* text)
{
    int length = strlen(text);
    char* result = malloc(length); // malloc adds the 0 automatically at result[length]
    int i;
    for (i = 0; i < length; i++)
        if ((text[i] >= 65) && (text[i] <= 90))
            result[i] = text[i] | 32;
        else
            result[i] = text[i];
    return result;
}

toLowerCase("hElLo WoRlD ÄäÖöÜü");
// result is "hello world ÄäÖöÜü"

tolower() from ctype.h doesn't do it either.

I think you need to read about setlocale , ( or here )

and use tolower()

Notes: During program startup, the equivalent of setlocale(LC_ALL, "C"); is executed before any user code is run.

You can try the Environment's default locale (or select the correct if you know it):

setlocale(LC_ALL, "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM