今天又写了一段装B的代码

Jinhao Jinhao 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在某群里,一MM问了一问题

从键盘任意输入一个字符串,把这个字符串中出现的第一个字母“a”替换成“xyz”,然后在字符串倒数第一个字符前面插入“this is my work”,最后求这个新字符串长度,并输出新字符串。这个题怎么做

没事干,就写了一下


#include <iostream>
#include <string>

int main()
{
using namespace std;
string str;
cin>>str;

try
{
string::size_type pos;

cout<<
str.replace((pos = str.find_first_of("a"), (pos == string::npos ? throw 0 : pos)), 1, "xyz").insert(str.size() + 1, "this is my work").size()
<<endl<<str<<endl;
}
catch(int)
{
cout<<str.insert(str.size() - 1, "this is my work").size()<<endl<<str<<endl;
}
}


MM回:错误一大堆

我无颜以对
...全文

相关推荐

评论 60
收藏 已收藏
分享
返回顶部
Jinhao
C++ 语言 65,214 成员 250,514 内容