Question

4.10 LAB: Name format Many documents use a specific format for a persons name. Write a program whose input is: firstName midfor c++ pls

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have provided the proper commented and indented code with proper screenshots, so that it may further help you to indent the code. Here we have used string streams to separate different parts of the name. String streams are similar to standard input stream just we take the input as strings here.

NOTE: You can compile the code using "g++ filename.cpp" command and run the same with "./a.out" command without the quotes.

CODE:

-----------------------------------------------------------------------------------------------------------

//Including the necessary libraries for I/O and stringstreams
#include<iostream>
#include<sstream>
using namespace std;

//The main Function
int main(){
    //Declaring the string to take the input name
    string str;

    //Taking the name as a whole line
    getline(cin,str);

    //declaring the string array to store different part of the names
    string name[3];

    //count variable to keep track of how many parts does the name have
    int count_name = 0;

    //Initializing the stringstream to take the input from the name i.e 'str'
    //and to seperate different parts of the name
    stringstream iss(str);

    //temporary string to store the part of name
    string temp;

    //Extracting different parts of name from the str with the help of loop
    while(iss>>temp){
        name[count_name] = temp;
        count_name++;
    }
    //Checking the different conditions for the number of subparts of the name
    //and printing the appropriate result
    if(count_name == 1){
        cout<<name[0]<<endl;
    }
    else if(count_name == 2){
        cout<<name[1]<<", "<<name[0]<<endl;
    }
    else if(count_name == 3){
        cout<<name[2]<<", "<<name[0]<<" "<<name[1][0]<<"."<<endl;
    }
    return 0;
}

-----------------------------------------------------------------------------------------------------------

Screenshots:

2 3 //Including the necessary libraries for 1/0 and stringstreams #include<iostream> #include<sstream> using namespace std; /

// Checking the different conditions for the number of subparts of the name //and printing the appropriate result if(count_na

Output:

Pan Silly Doe Doe, Pan s.

Julia Clarke Clarke, Julia

Happy Coding!

Add a comment
Know the answer?
Add Answer to:
for c++ pls 4.10 LAB: Name format Many documents use a specific format for a person's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in c++Many documents use a specific format for a person's name. Write a program whose input...

    in c++Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName, and whose output is: lastName, firstName middleInitial. Ex: If the input is: Pat Silly Doe the output is: Doe, Pat S. If the input has the form firstName lastName, the output is lastName, firstName. Ex: If the input is: Julia Clark the output is: Clark, Julia

  • 1.15 LAB: Input: Welcome message Write a program that takes a first name as the input,...

    1.15 LAB: Input: Welcome message Write a program that takes a first name as the input, and outputs a welcome message to that name. Ex: If the input is Pat, the output is: Hello Pat and welcome to CS Online! Python Language

  • Part 2: Processing Strings (Individual work) Processing user-entered data to follow a specific format is a...

    Part 2: Processing Strings (Individual work) Processing user-entered data to follow a specific format is a common task. Often this involves using string functions to manipulate a set of input strings. Create a MATLAB script named namephone.m and place these lines of code at the beginning: name  = input('Enter your first and last name: ','s'); phone = input('Enter your area code and phone number: ','s'); Tasks Here are useful string functions:  length, strcat, strtrim, lower, upper, strcmp, findstr, strrep As you work...

  • C++ Code Please, Three employees in a company are up for a special pay increase. You...

    C++ Code Please, Three employees in a company are up for a special pay increase. You are given a file, say Ch3_Ex5Data.txt, with the following data: Miller Andrew 65789.87 5 Green Sheila 75892.56 6 Sethi Amit 74900.50 6.1 Each input line consists of an employee’s last name, first name, current salary, and percent pay increase. For example, in the first input line, the last name of the employee is Miller, the first name is Andrew, the current salary is 65789.87,...

  • I need help with this program using c++ language! CS 317 Program Assignment Name Arrange You...

    I need help with this program using c++ language! CS 317 Program Assignment Name Arrange You are to write a program that will read in each person’s name and print out the results. The list has been collected form different places and the names are not in some standard format. A person’s last name may appear last or it may appear first. A label has been associated with each name to identify where the last name appears. Either “surname” or...

  • 4.16 LAB: Password modifier Many user-created passwords are simple and easy to guess. Write a program...

    4.16 LAB: Password modifier Many user-created passwords are simple and easy to guess. Write a program that takes a simple password and makes it stronger by replacing characters using the key below, and by appending "q*s" to the end of the input string. • i becomes ! • a becomes @ • m becomes M • B becomes 8 • o becomes Ex: If the input is: mypassword the output is: Mypassw.rdq*s Hint: Python strings are immutable, but support string concatenation. Store and build the stronger password in the given password...

  • Write in C++ 1. Use the following Person class (Person.cpp, Person.h). You will be writing Person...

    Write in C++ 1. Use the following Person class (Person.cpp, Person.h). You will be writing Person objects to a random access file. --------------------- Person.cpp--------------------------------- // Class Person stores customer's credit information. #include <string> #include "Person.h" using namespace std; // default Person constructor Person::Person( int idValue, string lastNameValue, string firstNameValue, int AgeValue ) { setID( idValue ); setLastName( lastNameValue ); setFirstName( firstNameValue ); setAge( AgeValue ); } // end Person constructor // get id value int Person::getID() const { return id;...

  • C programming. please include comments In this lab, you will learn to read data from and...

    C programming. please include comments In this lab, you will learn to read data from and write data to a file - another use for pointers. SAMPLE PROGRAM There are two sample programs this week - WriteFile.c and ReadFile.c. The first, WriteFile.c, allows the user to enter customer names (first, middle, and last), and writes each name to an output text file, customerNames.txt. formatted as: firstName middlelnitial lastName" After you've run it once, what happens to the names you added...

  • Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this...

    Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...

  • For this lab assignment, you will be writing a few classes that can be used by...

    For this lab assignment, you will be writing a few classes that can be used by an educator to grade multiple choice exams. It will give you experience using some Standard Java Classes (Strings, Lists, Maps), which you will need for future projects. The following are the required classes: Student – a Student object has three private instance variables: lastName, a String; firstName, a String; and average, a double. It has accessor methods for lastName and firstName, and an accessor...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT