-2

I am loosing my hair right now. I have a string, which i manipulate to start a new line/sentence after a punctuation, but I cant understand how I can capitalize the first word of each sentence? Except for this, i can not get out of the loop to change the dot into a dot and new line.

int main()
{
    string const txt1 = "Candy is good for your health.";
    string const text2 = "All kids should buy candy.";
    string const text3 = "Candy nowadays is a hit among kids.";
    string const text4 = "Every meal should include candy.";


    string text = text1 + text2 + text3 + text4;

    transform(text.begin(), text.end(), text.begin(), ::tolower);

    while (text.find("candy") != string::npos)
        text.replace(text.find("candy"), 3, "fruit");
    string_replace_all(text, ".", ".\n");

This is what i have added so far:

string line, total = ""; istringstream stream(text);
while (getline(stream, line, '\n'))
{
    if (line.size() > 0)
        total += (char)toupper(line[0]) + line.substr(1) + "\n";
    else total += "\n";
}
Thesar
  • 57
  • 1
  • 1
  • 9

1 Answers1

1

A very simply way to this would be:

string line, total = ""; istringstream stream(someString);
while(getline(stream, line, '\n')) 
{
    if(line.size() > 0) 
        total += (char)toupper(line[0]) + line.substr(1) + "\n";
    else total+= "\n";
}

Hope this helps.

Jake Freeman
  • 1,700
  • 1
  • 8
  • 15