Question

In: Computer Science

Must be in C++ and we can not use STR[] we must use STR.at() Write a...

Must be in C++ and we can not use STR[] we must use STR.at()

Write a program that:

  1. Declares a variable suitable for holding a person’s name
  2. Prompts the user to enter a person’s name with the text Enter name:
  3. Reads the user's input and stores it in the variable created in step 1
  4. Outputs the following information about the name they entered
    a. The index of the last character in the name
    b. The last 3 characters of the first name
    c. The first 3 characters of the last name
    Your output should look like the examples below. **Note: Your code should account for the fact that the user might enter a middle name or initial.

Example 1:

Enter name: Grace Hopper
Index of last character: 11
Last 3 characters of first name: ace
First 3 characters of last name: Hop

Example 2:

Enter name: Dorothy J. Vaughan
Index of last character: 17
Last 3 characters of first name: thy
First 3 characters of last name: Vau

This is my code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   string userFirstname;
   string userLastname;
   string userMiddlename;

   cout << "Enter Name: ";
   cin >> userFirstname;
   cin >> userMiddlename;
   cin >> userLastname;
   cout << userFirstname << userMiddlename << userLastname << endl;

   cout << "Index of last character: ";
   cout << ((userFirstname.size() + userLastname.size()) - 1);
   cout << endl;

   cout << "Last 3 characters of first name: ";
   cout << (userFirstname.at(userFirstname.size() - 3)) << (userFirstname.at(userFirstname.size() - 2)) << (userFirstname.at(userFirstname.size() - 1)) << endl;

   cout << "First 3 characters of last name: ";
   cout << userLastname.at(1) << userLastname.at(2) << userLastname.at(3) << endl;

   return 0;
}

Solutions

Expert Solution

// do comment if any problem arises

//there is some problem in your input method because middle name is optional so use getline()

#include <iostream>

#include <string>

using namespace std;

// this method finds first space that occured in given string

int first_space(string temp)

{

    for (int i = 0; i < temp.size(); i++)

    {

        if (temp.at(i) == ' ')

            return i;

    }

    return -1;

}

// this method finds last space that occured in given string

int last_space(string temp)

{

    int last = -1;

    for (int i = 0; i < temp.size(); i++)

    {

        if (temp.at(i) == ' ')

            last = i;

    }

    return last;

}

int main()

{

    string username;

    cout<<"Enter Name: ";

    getline(cin, username);

    cout << username << endl;

    cout << "Index of last character: ";

    cout << (username.size() - 1)<<endl;

    cout << "Last 3 characters of first name: ";

    // find ending index of first name

    int first=first_space(username);

    // find starting index of last name

    int last=last_space(username);

    cout << (username.at(first - 3)) << (username.at(first - 2)) << (username.at(first - 1)) << endl;

    cout << "First 3 characters of last name: ";

    cout << username.at(last+1) << username.at(last+2) << username.at(last+3) << endl;

    return 0;

}

Output:


Related Solutions

You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program as follows: Ask the user for an integer representing a number of integers to be sorted. Create an array of integers of the size provided by user. Initialize the array to zeros. Ask the user for and populate the array with user input. Output the array elements on one line separated by a space. Write a function name “supersort” that takes an integer pointer...
Can you tell me what is wrong with this code ? def update_char_view(phrase: str, current_view: str,...
Can you tell me what is wrong with this code ? def update_char_view(phrase: str, current_view: str, index: int, guess: str)->str: if guess in phrase: current_view = current_view.replace(current_view[index], guess) else: current_view = current_view    return current_view update_char_view("animal", "a^imal" , 1, "n") 'animal' update_char_view("animal", "a^m^l", 3, "a") 'aamal'
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of...
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of x students’ grades (grades will be stored in an array). To do so, please follow these guidelines: - Your program should ask the user for the number of students that are in the class. This number should help you declare your array. - Use the function seen in class to scan the grades of the array. In other words, we will populate the array...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table size: 10 N N^2 Square root of N --------------------------------------------- 1 1 1.00 2 4 1.41 3 9 1.73 ….. 10 100 3.16
using java language only write a code Have the function StringChallenge(str) take the str string parameter...
using java language only write a code Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 ------- you have the following code edit it to get the result mport java.util.*; import java.io.*; class Main {   public static String StringChallenge(String str)...
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT