/* * @topic T00251 <span style="background-color: yellow;">New: </span> program that converts string to uppercase * @brief All functions in one file */ // This demo program converts string "Hello" to its // uppercase equivalent, "HELLO". #include <iostream> #include <string> using std::string; using std::cout; void pause() { //---------------------------------------------- // prevent window from closing before exiting: //---------------------------------------------- char ch; // ch is a variable that holds a character // curious \n is a "line feed" control character, // it moves the cursor to the next line: std::cout << "\nEnter X to Exit: "; std::cin.ignore( 1, '\n' ); // extract and discard characters std::cin.clear(); // this clears cin state std::cin.get( ch ); // this reads a character from the keyboard, // and pauses the program from before exiting } // This function takes string parameter and returns the uppercase // string back to the caller: string to_uppercase( string str ) { // for each character, int idx = 0; for ( ; idx < str.length(); ++idx ) { char ch = str[ idx ]; str[ idx ] = toupper( ch ); } // replace lowercase chars with uppercase eq return str; } int main() { string str; str = "Hello"; // 0123456789 str = to_uppercase( str ); cout << str << '\n'; pause(); return 0; }