Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i am trying to convert characters in a string from capital letter to small letter using for loop.it's not converting into small letter in output

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading