我使用 C 编写了一个将大写字母转换为小写字母的函数。
#include <stdio.h>
#include <stdlib.h>
//İi Iı Ğğ Şş Çç Üü Öö
char toUpLow(char letter)
{
if (letter >= 'A' && letter <= 'Z') {
return letter - 'A' + 'a';
}
else if (letter >= 'a' && letter <= 'z') {
return letter - 'a' + 'A';
}
else {
return -1;
}
}
int main()
{
char myChar;
printf("Enter a character: ");
scanf("%c", &myChar);
printf("%c", toUpLow(myChar));
return 0;
}
我想添加土耳其字母。
char toUpLow(char letter)
{
if (letter == 'İ') {
printf("i");
}
else if (letter == 'i')
{
printf("İ");
}
else if (letter == 'I')
{
printf("ı");
}
else if (letter == 'ı')
{
printf("I");
}
else if (letter == 'Ğ')
{
printf("ğ");
}
else if (letter == 'ğ')
{
printf("Ğ");
}
else if (letter >= 'A' && letter <= 'Z') {
return letter - 'A' + 'a';
}
else if (letter >= 'a' && letter <= 'z') {
return letter - 'a' + 'A';
}
else {
return -1;
}
}
我尝试使用
if
/ else
添加土耳其语字母,但收到此错误:
uplowfunction.c:22:24: warning: multi-character character constant [-Wmultichar]
有办法做到这一点吗?
错误消息表明您没有对土耳其字母使用单字节编码,例如 ISO8859-9、Windows 代码页 1254 或 MS/DOS 代码页 857。您可以对非 Unicode 代码点使用 UTF-8 编码。 ASCII 字符使用 2 到 4 个字节的序列表示。
非 ASCII 字符不能在字符常量中使用,或者更准确地说,不应该在字符常量中使用,因为它们会被解析为多字符常量,这容易出错且不可移植。
要转换 UTF-8 字符串中的大小写,您应该使用宽字符或转换完整字符串而不是单个字符。请注意,转换后的字符串的长度可能与原始字符串的长度不同:
strlen("İ") != strlen("i")