to converting upper and lower case why we add +32 and -32 pleas explain
to converting upper and lower case why we add +32 and -32 pleas explain
Here is a code snippet and I didn't get why +32 and -32 is getting add or subtract to make characters upper case lower case.
/******** function definition *******/ void stringLwr(char *s) { int i=0; while(s[i]!='\0') { if(s[i]>='A' && s[i]<='Z'){ s[i]=s[i]+32; } ++i; } } void stringUpr(char *s) { int i=0; while(s[i]!='\0') { if(s[i]>='a' && s[i]<='z'){ s[i]=s[i]-32; } ++i; } }
for more reference, I was learning it on a website C program to convert in Upper and Lower case.
Answer by artm for to converting upper and lower case why we add +32 and -32 pleas explain
Because in ASCII code, a
- A
= 32
( A
= 65, a
= 97 ), so the space between any upper case and lower case of the same character (like a
and A
, or b
and B
, and so on) is 32.
Answer by Yoav for to converting upper and lower case why we add +32 and -32 pleas explain
the ascii value of A is 65 and a is 97 therefore, 65 + 32 = 97 take you to lower case. 97 - 32 = 65 take you to upper case.
you can look up http://www.asciitable.com/
Answer by coderredoc for to converting upper and lower case why we add +32 and -32 pleas explain
All the mystery is in the ASCII code.. Code of a = 65 A=97
SO now changing just means adding or subtracting 32. That's it.
Difference a=65 A=97 32 b=66 B=98 32 c=67 C=99 32 d=68 D=100 32 ... ... .. ..
By the way this is part of any basic C/C++ book.
Now in case you want to change from 'a' to 'A' you have to subtract 32 becuase that's what the ascii code for 'A' corresponds.
Similar is the logic for 'A' to 'a' (add 32).
Another thing is that 'a' gives us the numeric ascii value.
printf("%c", 66); ===> b printf("%d", 'c');===> 67
Answer by R Sahu for to converting upper and lower case why we add +32 and -32 pleas explain
The ASCII codes for a
-z
are 97-122.
The ASCII codes for A
-Z
are 65-90.
To get A
from a
, you need to subtract 32.
To get a
from A
, you need to add 32.
To make the code a little more clear, you can use:
s[i] += ('a' - 'A'); // 'a' = 'A' + ('a' - 'A');
when converting an uppercase letter to a lowercase letter and
s[i] += ('A' - 'a'); // 'A' = 'a' + ('A' - 'a');
when converting a lowercase letter to an uppercase letter.
Answer by Keith Thompson for to converting upper and lower case why we add +32 and -32 pleas explain
As the other answers have said, it's because the ASCII representation of each lower case letter happens to be 32 greater than the representation of the corresponding lower case letter.
It's rarely a good idea to depend on this. There are character sets other than ASCII; in EBCDIC, for example, this will not work.
The standard functions toupper
and tolower
, declared in
, will portably convert characters to upper or lower case (and will map non-letters to themselves). Rather than
if(s[i]>='A' && s[i]<='Z'){ s[i]=s[i]+32; }
you can write:
s[i] = toupper((unsigned char)s[i]);
(The cast to unsigned char
is needed because of an unfortunate glitch in the way these functions are defined.)
Answer by Friedrich for to converting upper and lower case why we add +32 and -32 pleas explain
Why does one not use the functions for that from ctype.h?
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment