In: Computer Science
Part I: Prompt the user for a single string and store it in a variable named userString.
a. Use a for loop to print the string, char by char, with a dash '-' char between each.
b. Use a for loop to print the string backwards, char by char, with a dash '-' char between each.
Part II: Create an array of 5 strings named userStrings. Use a generalized array size.
const int n = 5; string userStrings[n];
a. Populate the array with user input in a for loop.
b. Print the results in a double nested for loop, char by char, with an asterisk '*' char between each letter in order.
(Remember to use the getline() function to gain string input from the user.)
// Program Template in c++
#include <iostream>
#include <string>
using namespace std;
//Function Prototypes
bool runAgain(void);
int main() {
const int n = 5;
string userString,userStrings[n];
do {
cout << "Please type a
single string: ";
getline(cin, userString);
for (int i = 0; i <
userString.length() - 1; i++) {
cout <<
userString[i] << "-";
}
cout <<
userString[userString.length() - 1] << endl;
for (int i = userString.length() -
1; i > 0; i--) {
cout <<
userString[i] << "-";
}
cout << userString[0]
<< endl;
cout <<
"Please type five strings: ";
getline(cin,
userStrings[n]);
}
for (int i = 0; i < n; i++)
{
for (int j =
0;j< userStrings[i].length() - 1; j++) {
cout << userStrings[i][j] <<
"*";
}
cout <<
userStrings[i][userStrings[i].length() - 1] << endl;
}
} while (runAgain());
return(0);
}
// Function Implementation
bool runAgain(void) {
char userResponse;
cout << "\nWould you like to run again (y or
n): ";
cin >> userResponse;
cin.ignore(); // to clean up the input stream
if (userResponse == 'y' || userResponse ==
'Y')
return(true);
return(false);
}
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
#include <iostream>
#include <string>
using namespace std;
//Function Prototypes
bool runAgain(void);
int main() {
const int n = 5;
string userString, userStrings[n];
do {
//Reading the single string
cout << "Please type a single string: ";
getline(cin, userString);
//Loop through each character in userString from start to end untill second last character
for (int i = 0; i < userString.length() - 1; i++) {
cout << userString[i] << "-"; //display the character and a dash after that
}
cout << userString[userString.length() - 1] << endl; //displaying the last character in userString
//Loop through each character in userString from second last to start untill second character
for (int i = userString.length() - 1; i > 0; i--) {
cout << userString[i] << "-"; //display the character and a dash after that
}
cout << userString[0] << endl; //displaying the first character in userString
//Reading the five string and adding to array userStrings
cout << "Please type five strings: \n";
for (int i = 0; i < n; i++) {
getline(cin, userStrings[i]);
}
for (int i = 0; i < n; i++) { //Loop through each strings in array userStrings
for (int j = 0;j< userStrings[i].length() - 1; j++) { //Loop throiugh each character in each strings
cout << userStrings[i][j] << "*"; //display the character and an asterik after that
}
cout << userStrings[i][userStrings[i].length() - 1] << endl; //displaying thge last character
}
} while (runAgain()); //Loop untill runAgain returns true
return(0);
}
// Function Implementation
bool runAgain(void) {
char userResponse;
//Ask the user whether want to run again or not
cout << "\nWould you like to run again (y or n): ";
cin >> userResponse; //Reading user response
cin.ignore(); // to clean up the input stream
if (userResponse == 'y' || userResponse == 'Y')
return(true); //return true if userResponse is Y/y
return(false); //else return false
}
OUTPUT: