C++ 如何将字符串中每个 sentence/line 的第一个单词大写?
C++ How to capitalize first word of each sentence/line in a string?
我现在正在脱发。我有一个字符串,我操纵它在标点符号后开始一个新的 line/sentence,但我不明白如何将每个句子的第一个单词大写?除此之外,我无法跳出将点更改为点和换行的循环。
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");
这是我目前添加的:
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";
}
一个非常简单的方法是:
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";
}
希望对您有所帮助。
我现在正在脱发。我有一个字符串,我操纵它在标点符号后开始一个新的 line/sentence,但我不明白如何将每个句子的第一个单词大写?除此之外,我无法跳出将点更改为点和换行的循环。
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");
这是我目前添加的:
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";
}
一个非常简单的方法是:
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";
}
希望对您有所帮助。