Ansi C - 将字符串转换为小写字符串

时间:2013-01-25 10:20:01

标签: c string lowercase

我需要将char数组转换为小写字符数组。我还想将一些特殊字符转换为Ä到ä。但是“ASCII-Code”196不在ASCII-128中。如何将它们转换为小写?我可以将它们用作字符串初始化程序,但无法真正处理它们的代码。如果这可能是编译器选项,我在没有c99模式的Linux上使用eclipse CDT(不能使用那个)。

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()

ctype.h也没有这样做。

1 个答案:

答案 0 :(得分:3)

我认为您需要了解setlocale,(or here

并使用tolower()

  

注意:在程序启动期间,相当于setlocale(LC_ALL,   “C”);在任何用户代码运行之前执行。

您可以尝试使用Environment的默认语言环境(如果您知道,也可以选择正确的语言环境):

setlocale(LC_ALL, "");