简体   繁体   English

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

[英]Ansi C - Convert String to Lower Case String

I need to convert a char array to a lower case char array. 我需要将一个char数组转换为小写char数组。 I also want to convert some special characters like Ä to ä. 我还想将Ä等特殊字符转换为ä。 But the "ASCII-Code" 196 isn't in the ASCII-128. 但是“ ASCII码” 196不在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). 如果这可能是一个编译器选项,那么我在没有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() from ctype.h doesn't do it either. 来自ctype.h tolower()也不这样做。

I think you need to read about setlocale , ( or here ) 我认为您需要阅读有关setlocale的信息 ,( 或在此处

and use tolower() 并使用tolower()

Notes: During program startup, the equivalent of setlocale(LC_ALL, "C"); 注意:在程序启动期间,等效于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, "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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