Q:

Rewrite the previous exercise using a position and length to manage the strings. This time use only the insert function

0

Rewrite the previous exercise using a position and length to manage the strings. This time use only the insert function. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <string>
#include <iostream>

std::string fixName(const std::string &name,
                    const std::string &prefix,
                    const std::string &postfix) {
  // An easy way.
  //std::string newName = prefix + " " + name + " " + postfix;
  //return newName;

  std::string newName = name;
  newName.insert(0, prefix + " ");
  return newName.insert(newName.size(), " " + postfix);
}

int main() {
  std::cout << fixName("James", "Mr.", "Jr.") << std::endl;

  std::string name, prefix, postfix;
  std::cin >> name >> prefix >> postfix;
  std::cout << fixName(name, prefix, postfix) << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now