Question

In: Computer Science

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:

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 the strings in two separate variables and output the strings. (2 pts)

Ex:

Enter input string:
Jill, Allen
First word: Jill
Second word: Allen


(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts)

Ex:

Enter input string:
Jill, Allen
First word: Jill
Second word: Allen

Enter input string:
Golden , Monkey
First word: Golden
Second word: Monkey

Enter input string:
Washington,DC
First word: Washington
Second word: DC

Enter input string:
q

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

#include<iostream>
#include<iomanip>
#include<sstream>
using namespace std;

int main(){
  
   string fullName;
  
   while(true){
       cout<<"Enter input string:\n";
       getline(cin, fullName,'\n');
      
       if(fullName.find(",")!=string::npos){
           stringstream ss(fullName);
           string firstWord, lastWord;
           getline(ss,firstWord,',');
           getline(ss,lastWord,',');
           cout<<"First word: "<<firstWord<<endl;
           cout<<"Second word: "<<lastWord<<endl;
          
       }else if(fullName.compare("q")==0){
           break;
       }
       else{
           cout<<"Error: No comma in string.\n";
       }
       cout<<endl;
   }


   return 0;
}


Related Solutions

(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...
(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...
6.6 Parsing strings (Java) (1) Prompt the user for a string that contains two strings separated...
6.6 Parsing strings (Java) (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) 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. (2 pts)...
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...
use c++ (1) Prompt the user for a title for data. Output the title. (1 pt)...
use c++ (1) Prompt the user for a title for data. Output the title. (1 pt) Ex: Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored (2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt) Ex: Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels (3) Prompt the...
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'...
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...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the getNumOfCharacters() method, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of...
8.30 LAB*: Program: Authoring assistant. PYTHON 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT