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
  • 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...

  • 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...

  • 22.39 LAB 13 C FALL 2019 Overview Demonstrate your ability to use pandas with functions Description...

    22.39 LAB 13 C FALL 2019 Overview Demonstrate your ability to use pandas with functions Description Write a program that reads data from an input file using a DataFrame and displays a subset of data using a method Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format: mpg, cylinders, displacement, horsepower, weight, acceleration, model_year, origin, name 18,9,307,130,3504, 12, 70, usa, chevrolet chevelle malibu 15,8,350,165,3693, 11.5,70, usa, buick skylark...

  • CSCI 112 C Programming Database Lab This lab will focus on the use and manipulation of...

    CSCI 112 C Programming Database Lab This lab will focus on the use and manipulation of structures. Each section of the lab should be designed within its own function, passing parameters as necessary. You are to construct a C program, database.c, that will retrieve and manipulate a company's payroll database, payfile.txt. The data for each employee should be read into a structure containing the following field identifiers: first : 7 characters maximum, initial : 1 character maximum, last : 9...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

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
ADVERTISEMENT