C++ C++;替换字符串中的字符
我目前正试图大写字母“i”,但前提是它本身 这是我的密码C++ C++;替换字符串中的字符,c++,C++,我目前正试图大写字母“i”,但前提是它本身 这是我的密码 int main() { string textMessage = "Yesterday’s Doctor Who broadcast made me laugh and cry in the same episode! i can only wonder what the Doctor will get into next. My family and i are huge fans."; replac
int main()
{
string textMessage = "Yesterday’s Doctor Who broadcast made me
laugh and cry in the same episode! i can only wonder what the
Doctor will get into next. My family and i are huge fans.";
replace(textMessage.begin(), textMessage.end(), 'i', 'I');
cout << textMessage;
}
intmain()
{
string textMessage=“昨天的广播医生让我
笑和哭在同一集!我只想知道
医生将进入下一个节目。我的家人和我都是超级粉丝。”;
替换(textMessage.begin(),textMessage.end(),'i','i');
cout一个字母本身是一个被空格包围的字母,而不是其他字母。一个字母本身也很容易解释,因为它总是被空格包围
replace(textMessage.begin(), textMessage.end(), ' i ', ' I ');
如果您将程序中的代码更改为此(它将用大写i替换带有空格的i)它应该可以工作。您不必使用replace函数,而是必须遍历字符串,只检查前面和后面有空格的那些“i”,并将它们替换为“i”。我想下面的代码段将实现您想要实现的功能:-
for (int iTraverse = 0; iTraverse< textMessage.length(); iTraverse++)
{
if (textMessage[iTraverse] == 'i' && textMessage[iTraverse-1] == ' ' && textMessage[iTraverse+1] == ' ')
{
textMessage[iTraverse] = 'I';
}
}
for(int-iTraverse=0;iTraverse
拿出一张纸,用简短的句子写下一个逻辑过程,确定字符串中的字母i
是单独的,还是单词的一部分。这不应该超过一分钟左右……好的,时间到了!现在只需将你写下的内容直接翻译成代码。问题解决了。我试过但没有成功你的输出是什么?所有的“我”都是小写字母这正是我所需要的注意事项不是,但你应该自己做这件事,而不是依靠别人,因为这不是很难做到的:)我肯定知道你从何而来,我宁愿自己去弄清楚,但我知道我不是在寻找确切的答案,我只是想知道函数,我可以计算出其他的一切好吧,我明白你的意思了……:)