我正在尝试将字符的字符从大写字母转换为使用loop的小字母。

发布于 2025-02-13 19:46:20 字数 456 浏览 1 评论 0原文

中找不到任何错误

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2025-02-20 19:46:20

您的整个for循环被跳过,字符'\ 0'为零,您输入的任何可打印字符串都不会具有小于零的字符。相反,将条件更改为!=

for(i=0; a[i]!='\0'; i++){ ... }

或简单地a [i],因为0对false in C中

for(i=0; a[i]; i++){ ... }

也在C中,

此外,已经有一个函数可以为您执行此操作 。如果允许使用它,则是首选方法:

#include <ctype.h>
...

for(i=0;a[i];i++){
    a[i]=tolower(a[i]);
}

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 !=:

for(i=0; a[i]!='\0'; i++){ ... }

or simply a[i] since 0 evaluates to false in C

for(i=0; a[i]; i++){ ... }

Also, never use gets, it creates a security vulnerability for buffer overflows, use fgets 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:

#include <ctype.h>
...

for(i=0;a[i];i++){
    a[i]=tolower(a[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文