• C

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.
andyw27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddunleaCommented:
Hi andyw27,

#include <ctype.h>

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

Regards
0
manav_mathurCommented:
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
0
ddunleaCommented:
Don't use c < 65 etc. It's not in the least portable across locales. Better to use isupper. The following is a list of similar functions:

       #include <ctype.h>

       int isalnum(int c);
       int isalpha(int c);
       int isascii(int c);
       int isblank(int c);
       int iscntrl(int c);
       int isdigit(int c);
       int isgraph(int c);
       int islower(int c);
       int isprint(int c);
       int ispunct(int c);
       int isspace(int c);
       int isupper(int c);
       int isxdigit(int c);

DESCRIPTION
       These functions check whether c, which must have the value of an unsigned char or EOF, falls into a certain character class according to the current locale.

       isalnum()
              checks for an alphanumeric character; it is equivalent to (isalpha(c) || isdigit(c)).

       isalpha()
              checks  for  an  alphabetic  character; in the standard "C" locale, it is equivalent to (isupper(c) || islower(c)).  In some locales, there may be additional characters for which
              isalpha() is true--letters which are neither upper case nor lower case.

       isascii()
              checks whether c is a 7-bit unsigned char value that fits into the ASCII character set.  This function is a BSD extension and is also an SVID extension.

       isblank()
              checks for a blank character; that is, a space or a tab.  This function is a GNU extension.

       iscntrl()
              checks for a control character.

       isdigit()
              checks for a digit (0 through 9).

       isgraph()
              checks for any printable character except space.

       islower()
              checks for a lower-case character.

       isprint()
              checks for any printable character including space.

       ispunct()
              checks for any printable character which is not a space or an alphanumeric character.

       isspace()
              checks for white-space characters.  In the "C" and "POSIX" locales, these are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and  verti­
              cal tab ('\v').

       isupper()
              checks for an uppercase letter.

       isxdigit()
              checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

manav_mathurCommented:
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
0
ddunleaCommented:
Hi manav_mathur,

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

Regards
0
manav_mathurCommented:
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
0
ddunleaCommented:
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.
0
Jaime OlivaresSoftware ArchitectCommented:
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);
}
0
manav_mathurCommented:
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


0
ddunleaCommented:
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.
0
baboo_Commented:
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
0
Jaime OlivaresSoftware ArchitectCommented:
I agree, in my code will be better to change to:

key = getch();
if (islower(key))
   key = toupper(key);  
0
manav_mathurCommented:
> _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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.