How many people just cringed and stopped reading there?Originally Posted by Elhaz
*raises hand*
Quzah.
How many people just cringed and stopped reading there?Originally Posted by Elhaz
*raises hand*
Quzah.
Hope is the first step on the road to disappointment.
I've got a little tip for you to make your algorithm much cleaner. In ASCII code, the only difference between a letter and the same letter of the opposite case is the fifth bit of the character. Upper case numbers have a fifth bit value of 0, lower case of 1, and the rest of the bits are the same. So to change the case of a letter, all you need to do is flip the fifth bit of the number. How do you do that? With the binary exclusive OR operator (^). If you don't know about binary operators or the exclusive or operation, look them up. At any rate, here's my revision of your code:
Oooh, clean. If you didn't know, the << operator is used as the binary shift operator(google it )Code:// C++ A Beginer's Guide - Herbert Schildt // Module 3 Mastery Check Question # 11 // Case Converter #include <iostream> using namespace std; int main() { cout << "Case Converter\n\n"; cout << "Please type something (enter a \'.\' to quit):\n\n"; char input; int caseCounter = 0; // records number of coversions while (input != '.') { cin >> input; if( input >= 'A' && input <= 'z' ) { input ^= 1 << 5; //Flip the fifth bit of the char caseCounter++; } cout <<input; } cout << "\n\nThe number of case changes was: " << caseCounter << "\n\n"; }