Question

In: Computer Science

Part I: Prompt the user for a single string and store it in a variable named...

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);
}

Solutions

Expert Solution

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:


Related Solutions

Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code please (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and...
Create a function named getCreds with no parameters that will prompt the user for their username...
Create a function named getCreds with no parameters that will prompt the user for their username and password. This function should return a dictionary called userInfo that looks like the dictionaries below: # Administrator accounts list adminList = [ { "username": "DaBigBoss", "password": "DaBest" }, { "username": "root", "password": "toor" } ] Create a function named checkLogin with two parameters: the userInfo and the adminList. The function should check the credentials to see if they are contained within the admin...
Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
(1) Prompt the user for a string that contains two strings separated by a comma.
(In C)(1) Prompt the user for a string that contains two strings separated by a comma. Examples of strings that can be accepted:Jill, AllenJill , AllenJill,AllenEx:Enter input string: Jill, Allen(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. Ex:Enter input string: Jill Allen Error: No comma in string. Enter input string: Jill, Allen(3) Extract the two words from the input string and remove any spaces. Store...
In Java please. Prompt the user for two string inputs. Perform the following actions on these...
In Java please. Prompt the user for two string inputs. Perform the following actions on these inputs, and print the result of each action: Part I: Concatenate the two strings into one. Compare the two strings and check whether they are the same (ignore the case). Look up the method equalsIgnoreCase() from Java API Convert all the characters in a string to uppercase Look up the method toUpperCase() from Java API Part II: Replace all the 'd' characters with ‘e'...
use c++ (1) Prompt the user for a string that contains two strings separated by a...
use c++ (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex:...
(1) Prompt the user for a string that contains two strings separated by a comma. (1...
(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex: Enter input...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Write a Java program named, MultiTable, (MultiTable.java), with the following tasks: Prompt user to input the...
Write a Java program named, MultiTable, (MultiTable.java), with the following tasks: Prompt user to input the maximum number (as integer) Store a multiplication table for all combinations (with some specific eliminations) of value 0 through the maximum number (being entered) into a 2-D array Write a method named printTable to print out the MulitTable, with the following header, public static void printTable(int[][] multitable) In printTable(), when the value of the MultiTable is an odd number, print out Z Must use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT