将char数组元素转换为大写或小写 [英] convert char array elemnts to upper or lower case
本文介绍了将char数组元素转换为大写或小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于我的家庭作业,我们要求从用户处获取一个字符串,并且:
For my homework assignment we are asked to get a string from the user and:
- 将其全部转换为大写=已完成
- 将其全部转换为小写=已完成
- 将每个字符从上到下转换,反之亦然.(问题)
我不知道我的逻辑是错误的还是简单的解决方法.任何建议表示赞赏.
I do not know if my logic is wrong or if it's a simple fix. Any suggestions are appreciated.
#include <stdio.h>
int main()
{
int i;
char sentense [30];
printf("Please enter a sentence\n");
fgets(sentense, 30, stdin);
for(i=0; sentense[i] != '\0'; i++)
{
putchar(toupper(sentense[i]));
}
for(i=0; sentense[i] != '\0'; i++)
{
putchar(tolower(sentense[i]));
}
for(i=0; sentense[i] != '\0'; i++)
{
if(sentense[i] >='65' && sentense[i] <='90')
{
putchar(tolower(sentense[i]));
}
else if(sentense[i] >= '97' && sentense[i] <='122')
{
putchar(tolower(sentense[i]));
}
else
{
}
}
return 0;
}
推荐答案
您需要更改
if(sentense[i] >='65' && sentense[i] <='90')
到
if(sentense[i] >= 65 && sentense[i] <= 90)
和休息情况,因为我们想在这里比较整数值.
and rest case(s) as we want to compare the integer value here.
启用编译器警告,您的编译器应警告您有关错误.
Enable the compiler warning and your compiler should be warning you about the mistake.
或者,您也可以使用 isupper()
/ islower()
库函数也是如此.
Alternatively, you can also make use of isupper()
/islower()
library functions, too.
这篇关于将char数组元素转换为大写或小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文