[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-25
13
Medium Priority
?
212 Views
Last Modified: 2010-04-15

Hi,

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

TIA.
0
Comment
Question by:andyw27
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 5

Expert Comment

by:ddunlea
ID: 12675207
Hi andyw27,

#include <ctype.h>

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

Regards
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12675303
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
 
LVL 5

Accepted Solution

by:
ddunlea earned 2000 total points
ID: 12675358
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:manav_mathur
ID: 12675407
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
 
LVL 5

Expert Comment

by:ddunlea
ID: 12675464
Hi manav_mathur,

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

Regards
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12675492
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
 
LVL 5

Expert Comment

by:ddunlea
ID: 12675561
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12675571
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12675641
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
 
LVL 5

Expert Comment

by:ddunlea
ID: 12676080
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
 
LVL 3

Expert Comment

by:baboo_
ID: 12677407
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12677429
I agree, in my code will be better to change to:

key = getch();
if (islower(key))
   key = toupper(key);  
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12694120
> _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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question