C语言 C - 将大写字母转换为小写字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15708793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - Convert an uppercase letter to lowercase
提问by lche
A really simple program. I just want to turn an 'A' into an 'a', but output is giving me 'A'.
一个非常简单的程序。我只想将“A”变成“a”,但输出给了我“A”。
#include <stdio.h>
int main(void) {
putchar(lower('A'));
}
lower(a)
int a;
{
if ((a >= 65) && (a >= 90))
a = a + 32;
return a;
}
回答by Scott Olson
You messed up the second part of your ifcondition. That should be a <= 90.
你搞砸了if条件的第二部分。那应该是a <= 90。
Also, FYI, there is a C library function tolowerthat does this already:
另外,仅供参考,有一个 C 库函数tolower已经可以做到这一点:
#include <ctype.h>
#include <stdio.h>
int main() {
putchar(tolower('A'));
}
回答by spartacus
I believe you want <= 90
我相信你想要 <= 90
lower(a)
int a;
{
if ((a >= 65) && (a <= 90))
a = a + 32;
return a;
}
Although tolower would probably just save you the hassle unless you wanted to do this yourself. http://www.cplusplus.com/reference/cctype/tolower/
尽管 tolower 可能只会为您省去麻烦,除非您想自己这样做。http://www.cplusplus.com/reference/cctype/tolower/
回答by Dave Newman
In ASCII the upper and lower case alphabet are 0x20 apart from each other, so this is another way to do it.
在 ASCII 中,大写字母和小写字母彼此相距 0x20,因此这是另一种方法。
int lower(int a)
{
if ((a >= 0x41) && (a <= 0x5A))
a |= 0x20;
return a;
}
回答by MLeblanc
One way to make sure it is correct is by using character instead of ascii code.
确保它正确的一种方法是使用字符而不是 ascii 代码。
if ((a >= 65) && (a <= 90))
what you want is to lower a case. it's better to use something like if (a >= 'A' && a <= 'Z'). You don't have to remind all ascii code :)
你想要的是降低一个案例。最好使用类似if (a >= 'A' && a <= 'Z'). 你不必提醒所有的 ascii 代码 :)
回答by sachin_hg
You can convert a character from lower case to upper case and vice-versa using bit manipulationas shown below:
您可以使用位操作将字符从小写转换为大写,反之亦然,如下所示:
#include<stdio.h>
int main(){
char c;
printf("Enter a character in uppercase\n");
scanf("%c",&c);
c|=' '; // perform or operation on c and ' '
printf("The lower case of %c is \n",c);
c&='_'; // perform 'and' operation with '_' to get upper case letter.
printf("Back to upper case %c\n",c);
return 0;
}
回答by Anoop Kumar Sharma
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "Strlwr in C";
printf("%s\n",strlwr(string));
return 0;
}
use strlwr for lowering the case
使用 strlwr 降低大小写
回答by user1596193
If condition is wrong. Also return type for lower is needed.
如果条件不对。还需要较低的返回类型。
#include <stdio.h>
int lower(int a)
{
if ((a >= 65) && (a <= 90))
a = a + 32;
return a;
}
int _tmain(int argc, _TCHAR* argv[])
{
putchar(lower('A'));
return 0;
}
回答by vivek kandula
#include<stdio.h>
void main()
{
char a;
clrscr();
printf("enter a character:");
scanf("%c",&a);
if(a>=65&&a<=90)
printf("%c",a+32);
else
printf("type a capital letter");
getch();
}
回答by PounKumar Purushothaman
Use This code
使用此代码
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(){
char a[10];
clrscr();
gets(a);
int i,length=0;
for(i=0;a[i]!='##代码##';i++)
length+=1;
for(i=0;i<length;i++){
a[i]=a[i]^32;
}
printf("%s",&a);
getch();
}

