What is this? (stuff in toupper())

This is a discussion on What is this? (stuff in toupper()) within the C Programming forums, part of the General Programming Boards category; Code: char upper(char c){ if( c>='a' && c<='z') return c +='A' - 'a'; else return c; } What the heck ...

  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
    20,954
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    I know - that's why I was very careful with my wording.

    Thanks.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Yes, or you could

    c -= 32 ;

    or

    c -= ' ' ;

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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

    c &= 0xDF ;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21