This is a simple question about a program that receives five C language characters and determines whether they are uppercase, lowercase, or English characters.

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

Hello, I'm working on a program to determine if it's a capital letter, a lowercase letter, or an English letter by typing 5 characters, but when I try to repeat it with a for sentence, it keeps saying else is not an English letter, but I can't even do it 5 times.

int main() {

        char c;
        for (int i = 0; i < 5; i++) {
            scanf_s("%c", &c);

            if (c >= 'a' && c <= 'z') {
                printf ("lower case"\n");

            }
            else if (c >= 'A' && c <= 'Z') {
                printf ("uppercase letters").\n");

            }
            else {
                printf ("Not English\n");
            }
        }


    return  0;

c scanf

2022-09-20 15:02

1 Answers

The workaround is to use the scanf_s function by adding "%c" to the first column, such as "%c".

Read the previously answered article below why this is happening.

https://hashcode.co.kr/questions/12755/

#include <stdio.h>

int main()
{
    char c;

    for (int i = 0; i < 5; i++) {
        scanf_s(" %c", &c, 1);

        if (c >= 'a' && c <= 'z')
            printf ("lower case"\n");
        else if (c >= 'A' && c <= 'Z')
            printf ("uppercase letters").\n");
        else
            printf ("Not English\n");
    }

    return  0;
}


2022-09-20 15:02

If you have any answers or tips


© 2022 pinfo. All rights reserved.