Question: c++ Please design and then code the following menu-driven program. The Name Game: 1. Input Full Name 2. Display Name 3. Change my Name to
c++
Please design and then code the following menu-driven program.
The Name Game:
1. Input Full Name
2. Display Name
3. Change my Name to all Lower Case.
4. Count and display the number of words in name
5. Reverse the Name
6. Display Most Frequent Character
7. Exit Menu
Programming Rules:
Declare a character array in main. Use a separate function for each menu item. Pass the name around to each function. Use the C-string functions to manipulate the name.
Validate Input Full Name for not empty.
Keep offering the menu until they choose quit.
Appropriate use of functions required - no global variables - good programming style throughout.
Use Programs 10-12, 10-13 and 10-14 as examples.
10-12
Program 10-12 1 // This program uses a function to copy a C-string into an array. 2 #include
30 31 void stringCopy(char string1[], char string2[]) 32 { 33 int index = 0; // Loop counter 34 35 // Step through string1, copying each element to 36 // string2. Stop when the null character is encountered. 37 while (string1[index] != '\0') 38 { 39 string2[index] = string1[index]; 40 index++; 41 } 42 43 // Place a null character in string2. 44 string2[index] = '\0'; 45 }
10-13
Program 10-13 1 // This program uses the function nameSlice to cut the last 2 // name off of a string that contains the user's first and 3 // last names. 4 #include
6 7 void nameSlice(char []); // Function prototype 8 9 int main() 10 { 11 const int SIZE = 41; // Array size 12 char name[SIZE]; // To hold the user's name 13 14 cout << "Enter your first and last names, separated "; 15 cout << "by a space: "; 16 cin.getline(name, SIZE); 17 nameSlice(name); 18 cout << "Your first name is: " << name << endl; 19 return 0; 20 } 21 22 //************************************************************** 23 // Definition of function nameSlice. This function accepts a * 24 // character array as its argument. It scans the array looking * 25 // for a space. When it finds one, it replaces it with a null * 26 // terminator. * 27 //************************************************************** 28 29 void nameSlice(char userName[]) 30 { 31 int count = 0; // Loop counter 32 33 // Locate the first space, or the null terminator if there 34 // are no spaces. 35 while (userName[count] != ' ' && userName[count] != '\0') 36 count++; 37 38 // If a space was found, replace it with a null terminator. 39 if (userName[count] == ' ') 40 userName[count] = '\0'; 41 }
10-14
Program 10-14 1 // This program demonstrates a function, countChars, that counts 2 // the number of times a specific character appears in a string. 3 #include
6 int countChars(char *, char); // Function prototype 7 8 int main() 9 { 10 const int SIZE = 51; // Array size 11 char userString[SIZE]; // To hold a string 12 char letter; // The character to count 13 14 // Get a string from the user. 15 cout << "Enter a string (up to 50 characters): "; 16 cin.getline(userString, SIZE); 17 18 // Choose a character whose occurrences within the string will be counted. 19 cout << "Enter a character and I will tell you how many "; 20 cout << "times it appears in the string: "; 21 cin >> letter; 22 23 // Display the number of times the character appears. 24 cout << letter << " appears "; 25 cout << countChars(userString, letter) << " times. "; 26 return 0; 27 } 28 29 //**************************************************************** 30 // Definition of countChars. The parameter strPtr is a pointer * 31 // that points to a string. The parameter Ch is a character that * 32 // the function searches for in the string. The function returns * 33 // the number of times the character appears in the string. * 34 //**************************************************************** 35 36 int countChars(char *strPtr, char ch) 37 { 38 int times = 0; // Number of times ch appears in the string 39 40 // Step through the string counting occurrences of ch. 41 while (*strPtr != '\0') 42 { 43 if (*strPtr == ch) // If the current character equals ch... 44 times++; // ... increment the counter 45 strPtr++; // Go to the next char in the string. 46 } 47 48 return times; 49 }
Example:
1. Input Full Name: Billie Alice Williams
2. Display Name: Billie Alice Wiliams
3. Change to Lower: Your name has been converted to lower case.
2. Display Name: billie alice williams
4. Count: There are 3 words in your name.
5. Reverse Name: Your name has been reversed.
2. Display Name: smailliw ecila eillib
6. Most Letters: The most used letter in your name is l
7. Exit: Thank you for playing the game.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
