C 如何在字符串中大写每个句子/行的第一个单词
C++ How to capitalize first word of each sentence/line in a string?
我现在正在失去头发。我有一个字符串,我会在标点符号后操作以启动新的行/句子,但是我不明白如何将每个句子的第一个单词大写?除此之外,我无法离开循环将点更改为点和新线。
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";
}
希望这会有所帮助。
相关文章:
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何获取句子中单词的第一个字母*没有*指针,引用等
- 如何输入每个单词在字符串数组中的句子,直到用户按Enter键
- 用句子从单词打印单词
- 如何在文件中找到单词或句子
- 如何删除句子 c++ 中的整个单词
- 使用 getline 和字符串流检查句子中的单词是否在文件中
- 如何从包含多个句子的输入文件中表达单个单词.C
- 从输入的句子中构建单词列表
- C 如何在字符串中大写每个句子/行的第一个单词
- 动态分配的数组C 读取句子并打印单词
- 计数句子中单词的字符数
- 大写每个句子中的第一个单词
- 在 C++ 的句子中查找某个单词
- 句子中的单词顺序相反
- 如何使用 c++ 反转句子字符串中的每个单词,例如"I love computer science"
- 我如何将句子分成一个单词
- 如何在C++中迭代一个句子的单词
- 如何使用C 中的Regex在给定句子中的多个空格隔开的两个单词匹配
- C++字符[句子/单词]