我正在尝试将字符的字符从大写字母转换为使用loop的小字母。
中找不到任何错误
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char a[50];
int i;
setbuf(stdout,NULL);
printf("enter a string");
gets(a);
for(i=0;a[i]<='\0';i++){
if(a[i]>='A'&&a[i]<='Z'){
a[i]=a[i]+32;
}
}
printf("%s",a);
return EXIT_SUCCESS;
}
我在代码输出
输入字符串sdjnjj sdjnjj
i cannot find any error in the code
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char a[50];
int i;
setbuf(stdout,NULL);
printf("enter a string");
gets(a);
for(i=0;a[i]<='\0';i++){
if(a[i]>='A'&&a[i]<='Z'){
a[i]=a[i]+32;
}
}
printf("%s",a);
return EXIT_SUCCESS;
}
output
enter a string SDJnjj
SDJnjj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的整个
for
循环被跳过,字符'\ 0'
为零,您输入的任何可打印字符串都不会具有小于零的字符。相反,将条件更改为!=
:或简单地
a [i]
,因为0对false
in C中也在C中,
此外,已经有一个函数可以为您执行此操作 。如果允许使用它,则是首选方法:
Your whole
for
loop is skipped, character'\0'
is zero, any printable string you enter won't have characters less than zero. Instead, change the condition to!=
:or simply
a[i]
since 0 evaluates tofalse
in CAlso, never use
gets
, it creates a security vulnerability for buffer overflows, usefgets
instead.Furthermore, there's already a function that does this for you called
tolower
. It is the preferred method if you're allowed to use it: