Checking whether chars are uppercase or lowercase in c++

8.8k Views Asked by At

I'm a beginner to c++ and I'm stuck at the very beginning.

The problem is simple: a letter is the input and the output should be "it's uppercase" or "it's lowercase", or simply "error" when it's not a letter.

And that's where I got stuck.

#include <iostream>
#include <cstdlib>
using namespace std;


int main(){
char t;
cin>>t;
if (t==toupper(t))cout<<"UPPER";
else if(t==tolower(t))cout<<"LOWER";
else cout<<"ERROR";
return 0;

}

That's my code. I have never worked with chars before. It seems that the program cannot know whether it's text or a number/special symbol. How do I get it to tell me whether the letter is upper, lower or an error?

1

There are 1 best solutions below

0
m. c. On
int main(){
     char t;
     cin>>t;
     if (t>='A' && t<='Z') cout<<"UPPER";
     else if(t>='a' && t<='z') cout<<"LOWER";
     else cout<<"ERROR";
     return 0;
     }

Even a char is not character, toUpper() will return it as as, this is why your code not working.