toUpper/Lower one line function
I was doing an exercise and came up with a one line function for toUpper/Lower.
Just wanted to show everyone. Granted there is no validation on this. I guess just making sure the value of the ch is 65-90 or 97-122.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
using namespace std;
char toUpper(char ch);
char toLower(char ch);
int main()
{
cout << "This program uses an XOR mask to convert\n" <<
"a character toUpper or toLower. The mask is\n" <<
"00100000b or 0x20h. This works because the\n" <<
"5th bit is changed. 1 to 0 for toLower\n" <<
"and 0 to 1 for toUpper.\n" << endl;
cout << "toLower...." << endl;
for (int i = 65; i <= 90; i++)
cout << static_cast<char>(i) << " toLower is: " << toLower(i) << endl;
cout << endl;
cout << "toUpper...." << endl;
for (int i = 97; i <= 122; i++)
cout << static_cast<char>(i) << " toUpper is: " << toUpper(i) << endl;
cout << endl << "Press ENTER to continue...";
cin.get();
return 0;
}
char toUpper(char ch)
{
return (ch ^ 0x20);
}
char toLower(char ch)
{
return (ch ^ 0x20);
}
|
Nice reading, now I have to try to write code for those functions as well.
updated code thread... http://cplusplus.com/forum/general/36127/
Topic archived. No new replies allowed.