Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

Inputting a character as a capital letter whether the caps lock is on or off.


Hi,

When a user inputs a character is there anyway to force it to be upper case ?

TIA.
Avatar of ddunlea
ddunlea

Hi andyw27,

#include <ctype.h>

int toupper(int c);
int tolower(int c);

Regards
there are 2 cases
1) you want to check whether user has entered upper case or not
if(((int)c < 65) || ((int)c > 97))

2) you want to force uppercase
int toupper(c)

Manav
ASKER CERTIFIED SOLUTION
Avatar of ddunlea
ddunlea

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and how is <65 and all that not portable unless you going for not ascii data. In that case, Im not sue if other functions will work too.....

Manav
Hi manav_mathur,

é is 130. Which is > 97, but still lower case.

Regards
I dont see any character in your post.

I guess 130 coresponds to a accentuated 'e'....which I dont believe to be lowercase,
and on the other hand, what will toupper() do to it??I dont think the result will be what you expect

Manav
Hi manav_mathur,

I can see an accented e in my post. Must be a bug in the tool I'm using to post.

toupper will leave a character unchanged if it doesn't understand it, and whether it understands or not depends on the locale that your C library was built with. I don't see why you disagree with me on isupper, when you agree with me on toupper.
If you want to restrict uppercase at "input time" then you have to make your own input function.
Something like:

InputFunction(char *buffer, int len)
{
    char key;
    int i=0;

do {
  key = toupper(getch());   /* <---- Here control is done */
  if (key>32 && i<len) {
      putch(key);
      buffer[i++] = key;
  }
  else if (key==8 && i>0) {   /* backspace */
      puts("\b \b");
      i--;
  }
  else if (key==13) {   /* enter */
      buffer[i] = 0;   /* close string */
      break;
  }
  else
    while (kbhit())
      getch();
} while (true);
}
ddunlea,
I am unable to see the character. I think thats because my browser might not support those chars.

>and whether it understands or not depends on the locale that your C library was built with.
exactly, and you dont know about the poster.


advisory :
#define _toupper(__c)   ((__c) + 'A' - 'a')

Manav


Hi manav_mathur,

> exactly, and you dont know about the poster

That's my point. My solution doesn't make presumptions about the poster or his/her local. The toupper function converts a character to an uppercase character if it is a lower case character which has a corresponding uppercase character in the user's locale. Your solution on the other hand, will break on anything other than a-z inclusive i.e. _toupper('A') will return '!' which I do not believe is what the poster wants.
TIA:

ddunlea's solution is right.  Let's say your character is stored in the variable c.  This would do what you need:

c = toupper( c );

Make sure you #include <ctype.h>.

All this is what ddunlea said first.

baboo_

P.S. manav and ddunlea - don't want to horn in on anyone's answer...  Just clarifying.  Or maybe throwing my support behind one side of this discussion ;p
I agree, in my code will be better to change to:

key = getch();
if (islower(key))
   key = toupper(key);  
> _toupper('A') will return '!' which I do not believe is what the poster wants.

That was an advisory...meaning dont use it. Sorry if it didnt make sense.....ahppens frequently with my posts ;)

Manav