Question

In: Computer Science

By using the string function find to find the index of a space; the function length...

By using the string function find to find the index of a space; the function length to find the length of the string; and the function substr from a file form firstname, middlename, and lastname to lastname, middlename, and first name. Please help me this is c++ thank you

Solutions

Expert Solution

Answer:

Note: Please make a file of name test.txt and store in the D drive or if you store at another place then change the PATH = "D:\test.txt"; in the given program to your's location.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const string PATH = "D:\test.txt";

string alterName(string fullName)
{
   string first_Name = "", middle_Name = "", last_Name = "";

   int comma_index = fullName.find(',', 0);
   last_Name = fullName.substr(0, comma_index);
   fullName = fullName.substr(comma_index + 2);
   int space_index = fullName.find(' ', 0);
   first_Name = fullName.substr(0, space_index);
   if (space_index != -1)
   {
       middle_Name = fullName.substr(space_index + 1);
   }

   string alteredName = first_Name + " ";
   if (!middle_Name.empty())
   {
       alteredName += middle_Name + " ";
   }
   alteredName += last_Name;
  
   return alteredName;
}

int main()
{
   ifstream name_file;
   name_file.open(PATH.c_str());

   while (name_file.good()) {
       string fullName;
       getline(name_file, fullName);
       string name = alterName(fullName);
       cout << name << endl;
   }
   getchar();
}

Output:

Please give thumbsup, if you like it. Thanks.


Related Solutions

C question Using a for-loop: (a) calculate the length of a string, (b) find a word...
C question Using a for-loop: (a) calculate the length of a string, (b) find a word in a string
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input...
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input string will be a sentance ex: "The cat is on the chair and the bear is on the chair") -- returns a table (word varchar(max), count int) (Output is a table that shows how many times each word appeared in the sentance) Where each space-delimited “word” in the string appears in the table along with the number of times it appeared in the input...
The length of time it takes to find a parking space at 9 a.m. follows a...
The length of time it takes to find a parking space at 9 a.m. follows a normal distribution with a mean of five minutes and a standard deviation of two minutes. a) Based upon the empirical rule, which is the probability that it takes less than one minute to find a parking space? b) Based upon the empirical rule, which is the probability that it takes more than 7 minutes to find a parking space? c) Find the probability that...
The length of time it takes to find a parking space at 8am follows a normal...
The length of time it takes to find a parking space at 8am follows a normal distribution with a mean of 10 minutes and a standard deviation of 3 minutes. 1.Find the probability that it takes at most five minutes to find a parking space. 2.Find the probability that it takes at least 13 minutes to find a parking space. 3.Find the probability that it takes between 9 minutes to 11 minutes find a parking space. 4.25% of the time,...
Write a function that counts the colors in a string using JavaScript. String "The quick brown...
Write a function that counts the colors in a string using JavaScript. String "The quick brown fox jumped the blue fence and landed on red paint." This should return the number of colors. The colors you are looking for are "blue, green, brown, gray, black, brown, red, purple".
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
Using the formula for arc length of, find the arc length of  between the limits of ....
Using the formula for arc length of, find the arc length of  between the limits of . Write out the formula and the steps leading to the answer.
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function...
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function checks if two adjacent characters in the string are the same; if they are the same, then return True otherwise return False.   The function should ignore case and check for non-empty string.  If it's an empty string, then return the message 'Empty string'. Sample runs: print( isAdjacent('ApPle')) # True print( isAdjacent('Python')) # False print( isAdjacent('')) # Empty string
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT