// Copyright 1999 // College of Computer Science // Northeastern University Boston MA 02115 // This software may be used for educational purposes as long as this copyright // notice is retained at the top of all files // Should this software be modified, the words "Modified from Original" must be // included as a comment below this notice // All publication rights are retained. This software or its documentation may // not be published in any media either in whole or in part. /////////////////////////////////////////////////////////////////////////////// // Recitation 5: Using and writing conditional statements; logical expressions // STUDENT NAME: ¥?¥?¥ // STUDENT ID: ¥?¥?¥ // DATE: ¥?¥?¥ /////////////////////////////////////////////////////////////////////////////// // The standard include files that include traditional C and C++ headers and // many other Core Tools headers ... see CHeaders.h for additional details #include "IOTools.h" #include "Graphics.h" #include "Random.h" // Enter project specific include files here as well as classes and functions // that you choose to define in the main shell rather than in separate files // prototypes void Exercise1(); void Exercise2(); void Exercise3(); void Exercise4(); void Exercise5(); void Exercise6(); void Exercise7(); void Exercise8(); void Exercise9(); void Exercise10(); // Recitation 5: EXERCISES // definitions void Exercise1(){ // Ask the user to type in a string // Print the string backwards cout << "Print the string backwards" << endl; string s; // get the string RequestString("Type in a sentence:", s); int len = s.length(); // remember its length int i; for (i = 0; i < len; i++){ // traverse the string cout << s[len - 1 - i]; // i-th character from the end } cout << endl; // final end-line }; void Exercise2(){ // Palindrome is a word that is the same when read backwards // Some examples are: ewe, anna, hannah, lonelytylenol // Write a program that will read a string and print appropriately // "This is a palindrome" or "This is not a palindrome" }; int FirstChar(const string s, char c){ // Return the location of the first occurrence of the given character // in the given string // If it is not found, return -1 int len = s.length(); // get the length of the string int i; for(i = 0; i < len; i++){ if (s[i] == c){ // if the character is found, return i; // return its location } } // character not found return -1; }; void Exercise3(){ // Write a function FirstChar that uses two arguments - a string s and a char c // and returns the position of the first occurrence of char c in the string s // Write a program that will test this function five times. cout << "Type in a string and a character to find" << endl; cout << "Program will locate thhe first occurrence of this letter." << endl; string test; // string to run the test on char c; // character to find int n; // loop counter for three tests for (n = 1; n <= 3; n++){ RequestString("Next string: ", test); // get the string c = RequestChar("The character to find:"); // and the char to find // print the message cout << "Letter " << c << " found at location "; cout << FirstChar(test, c) << endl; // calling FirstChar function } }; void Exercise4(){ // Write a function that will return the location of the first letter // (i.e. 'a' through 'z' or 'A' through 'Z') // in a string typed in by the user // Write a program that will test this function }; void Exercise5(){ // Write a function that will count the number of occurrences of a given // character in a given string // Test this function }; void Exercise6(){ // Ask the user to type in a string // Print the first letter of every word in this string // (Try it with the sentence: Your eyes seems so incredibly red) cout << "This program will print the first letter of every word" << endl; cout << "in the string that you type in." << endl; string s; // get the string and its length RequestString("Type in a sentence:", s); int len = s.length(); int i = 0; // index for the string traversal while (i < len){ // traversing till the end of the string // look for the next non-space while ((i < len) && (s[i] == ' ')){ i++; }; // print it if (i < len) cout << s[i]; // look for the next space while ((i < len) && (s[i] != ' ')){ i++; }; }; // end of the 'big' while loop }; void Exercise7(){ // Write a program that will count the words in a string. }; void Exercise8(){ // Write a program that will compute the average length of a word }; void Exercise9(){ // Write a program that will replace every occurrence of a space // with a * }; void Exercise10(){ // Write a program that will replace every letter of a string // with its successor cout << "We will encrypt your message!" << endl; string s; // get the message to encrypt RequestString("Type in your message:", s); int len = s.length(); // remember its length int i; // loop counter char c; // encrypted character for (i = 0; i < len; i++){ // look at every letter in the string c = s[i] + 1; // set c to its successor cout << c; // and print it } cout << endl; }; int main(int argc, char* argv[]) { // Use the following line if you choose NOT to open any graphics windows // InitializeConsole(); // Build graphics window 0 GraphicsWindow GW0(300, 300); // Move the console below graphics window 0 ConsolePlaceBelow(0); // Give the console the focus for user interaction MakeConsoleForeground(); ////////// // Enter the main program here SetRandomSeed(); int i = 1; while (Confirm("Another exercise?", true)){ // loop to run another exercise i = RequestInt("Exercise number: ", i); // determine which exercise to run // the default moves you to the next one cout << endl << "Exercise " << i << endl; // notify user of the accepted selection switch (i){ // un the appropriate exercise case 1: { Exercise1(); break; } case 2: { Exercise2(); break; } case 3: { Exercise3(); break; } case 4: { Exercise4(); break; } case 5: { Exercise5(); break; } case 6: { Exercise6(); break; } case 7: { Exercise7(); break; } case 8: { Exercise8(); break; } case 9: { Exercise9(); break; } case 10: { Exercise10(); break; } default: Exercise1(); }; i = (i+1); // next time, continue with the next exercise if (i > 10) // we only have 10 exercises - cycle through again i = 1; cout << endl; } ////////// // The lines below make sure that the graphics windows remain open just // before the program terminates PressReturn("\nThe main program is about to terminate\n"); return 0; }