Home c++ Given a string. Replace the first letters of all words with capital...

Given a string. Replace the first letters of all words with capital letters (if the word begins with a capital letter, leave it unchanged)

Author

Date

Category

I solved the problem a little the wrong way, in my opinion. Can the problem be considered solved?


Answer 1, authority 100%

It depends on what is considered the right decision. And depending on what is considered a word 🙂
Let’s accept, as you have, that a word is something delimited by spaces.

Your solution gives a stream of words, but does not preserve the number of spaces. For example, AAA BBB will turn into AAA BBB . How critical it is – you know better. In addition, your original string will be “spoiled”, it will not contain the same words, just with different first letters. Your task is not to display the words, but to change them in the line , as I understand it.

I would have done it easier – just scan character by character, and all characters following the space (as well as the first character of the line) are capitalized.

Will you write the code yourself?


Answer 2, authority 96%

can be simpler, although a matter of taste

# include & lt; iostream & gt;
#include & lt; cctype & gt;
using namespace std;
int main () {
  cout & lt; & lt; "Enter words separated by spaces";
  string s;
  while (cin & gt; & gt; s) {
    s [0] = toupper (s [0]); \\ re-transform
    cout & lt; & lt; s & lt; & lt; "";
  }
  return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions