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

1. ## 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. 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.

3. 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

4. Originally Posted by Todd Burch
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. I know - that's why I was very careful with my wording.

Thanks.

6. so if i did c += -32;

should that work as well?

7. Yes, or you could

c -= 32 ;

or

c -= ' ' ;

Todd

8. If you wanted to get real obtuse, you could

c &= 0xDF ;

9. Another alternative; perhaps easier to understand?

10. But if you want to write portable code, you should use the standard toupper() & tolower() functions instead of trying to write your own.