Thread: What is this? (stuff in toupper())

  1. #1
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73

    What is this? (stuff in toupper())

    Code:
    char upper(char c){
    	
    	if( c>='a' && c<='z')
    	return c +='A' - 'a';
    		else return c;
    }
    What the heck does 'A' - 'a' do?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Return the difference in position of 'A' and 'a'. Assuming that the alphabetic char values are in alphabetical order (and that is true for ASCII and its successors), this computes the mapping from each lowercase letter to its uppercase equivalent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    In ASCII, the value of a blank is 0x20, or 32 decimal. If you look at an ASCII character chart, you'll see that each upper case and lowercase character are decimal 32 apart.

    A capital "A" is decimal 65. A lower case "a" is decimal 97.

    65 - 97 = -32.

    If c was lower case "a" (97) and you add -32, you get 65 (uppercase "A").

    Quite a convoluted way to do it, but it works.

    If the input is always a character in the range of a-z, the technique of adjusting by the value of a blank works for ASCII, and EBCDIC as well (although EBCDIC is in the other direction... the lower case sort ascending first).

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Todd Burch View Post
    In ASCII, the value of a blank is 0x20, or 32 decimal. If you look at an ASCII character chart, you'll see that each upper case and lowercase character are decimal 32 apart.

    A capital "A" is decimal 65. A lower case "a" is decimal 97.

    65 - 97 = -32.

    If c was lower case "a" (97) and you add -32, you get 65 (uppercase "A").

    Quite a convoluted way to do it, but it works.

    If the input is always a character in the range of a-z, the technique of adjusting by the value of a blank works for ASCII, and EBCDIC as well (although EBCDIC is in the other direction... the lower case sort ascending first).

    Todd
    Just to clarify: 'a' - 'A' might work for EBCDIC, but checking if the char is between ( 'a' && 'z' ) definitely won't work because the alphabet is fragmented in EBCDIC.
    http://www.legacyj.com/cobol/ebcdic.html

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I know - that's why I was very careful with my wording.

    Thanks.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    so if i did c += -32;

    should that work as well?

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes, or you could

    c -= 32 ;

    or

    c -= ' ' ;

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you wanted to get real obtuse, you could

    c &= 0xDF ;
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Another alternative; perhaps easier to understand?

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But if you want to write portable code, you should use the standard toupper() & tolower() functions instead of trying to write your own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. toupper() function
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2008, 10:54 AM
  2. Tab key stuff. C+WinAPI is killing me. Please help.
    By Templario in forum Windows Programming
    Replies: 5
    Last Post: 11-21-2002, 03:35 PM
  3. arguments, directories and stuff...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-26-2002, 05:46 PM
  4. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  5. Stocks 'n' stuff...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-20-2001, 05:36 PM