EN VI

Turning words into uppercase in C?

2024-03-10 18:30:05
How to Turning words into uppercase in C

I have this problem from school, (below is my code), i can turn the characters into uppeercase, but i dont know how to stop getting input, normally my teacher would give something like "the input ends when it encounter '1'", but this time his test case is just normal text:

the problem: Description Given a TEXT, write a program that converts the TEXT to upper-case.

Example

Input Hello John,

How are you?

Bye,

Output

HELLO JOHN,

HOW ARE YOU?

BYE,

my code:

#include <stdio.h>

int main(){
    char c;
    while(1){
        scanf("%c", &c);
        if(c == EOF) break;
        else{
            c = toupper(c);
            printf("%c", c);
        }
    }
    return 0;
}

my code proccesses uppercase request fine, but it never ends. Can you help me, thank you very much!

Solution:

scanf returns EOF

int main(){
    char c;
    int result;
    while(1){
        result = scanf("%c", &c);
        if(result == EOF) break;
        else{
            c = toupper(c);
            printf("%c", c);
        }
    }
    return 0;
}

You will not EOF until you type special keyboard key combination.

  • Linux: CTRL-D
  • Windows: CTRL-Z
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login