C++的编程应用

来源:百度知道 编辑:UC知道 时间:2024/06/05 09:52:30
设计一程序,它输入一字符串,如果其中有子串"BOY",将之替换为"CHILD"显示替换后的字符串
请写下怎么编的,如果我会还问来干吗

#include <string>
int main()
{
std::string str;
std::cout<<"请输入字符串"<<std::endl;
std::cin>>str;
for (std::string::size_type pos = str.find("boy"); pos != std::string::npos ; pos = str.find("boy",pos+1))
{
str.replace(pos,strlen("boy"),"child");
}
std::cout<<str<<std::endl;
return EXIT_SUCCESS;
}

这种程序很简单,楼主最好自己动脑筋想,我只提示你一下,if(a[n]=='B'&&a[n+1]=='O'&&a[n+2]=='Y'){};注意,a为字符数组,在大括号内要注意移动数组下标。