// Ch. 8 Demo #5 // Computer Science Using C++ // Mr. Minich // Purpose - to illustrate how to process an inputted string into single // separate characters and displaying two spaces between every // four characters (starting from the right.) #include<iostream.h> #include "M:\C++ Programming\AP classes\apstring.h" #include <stdlib.h> // necessary for system("CLS") command int main() { apstring inputtedString;// user's inputted string int stringLength = 0; // length of string int counter = 0; // position within string int spacePosition = 0; // adjusts for spacing cout << "Enter a string: "; cin >> inputtedString; stringLength = inputtedString.length(); spacePosition = 4 - stringLength % 4 + 1; // this works but maybe it could be more efficient // parentheses are not necessary since % is performed before - according to // the order of operations while (counter < stringLength) { cout << inputtedString[counter] << ' '; if (spacePosition % 4 == 0) { cout << " "; // two spaces necessary here } counter++; // moving to the next letter of string spacePosition++; } // ************************ Demo #2 ******************************** char anyChar; // user presses any key to continue char userBit = '0'; // inputted bit apstring binaryNumber; // concatenated string formed by inputted bits cout << "\n\nPress any non-white space character to continue to the next demonstration.\n"; cin >> anyChar; system("CLS"); // clears the screen cout << "Demonstrating how to concatenate characters into a string on the fly....\n" << endl; while (userBit != '9') { cout << "Enter a binary value, 0 or 1 (9 to quit): "; cin >> userBit; if (userBit != '9') { binaryNumber += userBit; // concatenating userBit to end of binaryNumber } } cout << "\nThe concatenated string is: " << binaryNumber << endl << endl; return 0; }// end of main