convert lower case to upper case

kensy11
kensy11 used Ask the Experts™
on
Hallo,

i am working on a program , it ask for a character and it gives an error if its a number

Now the next thing i want to do is if we give a character in lower case i want the program to convert it to upper case is it possible doing it, only using the #include <conio.h> ??

Thanks
#include <stdio.h>
#include <conio.h>
int main (void)

{
    int key;
    
    printf ( "give a key ");
    key = getch ();
     
     if (key >= '0' && key <= '9')
       printf ("\n\nError the key was not a character\n\n hit enter to close the program");
    
           if (key >= 'a' && key <= 'z')
              printf ("\n\nthe key was %c ",key);
    
    getchar();
    
    
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
evilrixSenior Software Engineer (Avast)

Commented:
You can use toupper to do this.
http://www.cplusplus.com/reference/clibrary/cctype/toupper/

You will need to enumerate through the string and uppercase each char.
string EventName;

EventName = txtEventName.Text;

EventName.ToUpper();

txtEventName.Text = EventName;
Hi kensy11,

since I guess you again want a solution without use of 'isupper' or 'toupper' (@evilrix FYI: http://www.experts-exchange.com/Programming/Languages/C/Q_26550959.html) you could do it like this:

> if ( key >= 'a' && key <= 'z' )
>  key = key - 'a' + 'A';

Hope that helps,

ZOPPO
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
thanks
yes, it should be possible,
use               printf ("\n\nupper case of the the key was %c ",key-32);
and this will give you the upper case.
ah, Zoppo has already given the answer. ! :)
evilrixSenior Software Engineer (Avast)

Commented:
The problem with doing it that way is it relies on the underlying character encoding being of a format where this will work and there is nothing to say this is the case. The toupper function is a standard C function that is guaranteed to correctly convert to upper case given the correct locale (if you use the C++ version).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial