判断字符ch是否与str所指串中的某个字符相同,若相同没,什么也不做,若不同,将其插入在最后

来源:百度知道 编辑:UC知道 时间:2024/05/11 02:08:26
用c++写,答案好的,给高分

strchr()函数

#include<iostream>
#include<string>

using namespace std;

int main()
{
char ex;
cout<<"请输入一个字符:";
cin>>ex;

string str("abcdefghigk");

int size = str.size();
cout<<"检测前:"<<str<<endl;
cout<<"字符串的长度是:"<<size<<endl;
bool sign = false;
int status = -1;

for(int i=0;i<size;i++)
{
if(ex == str[i])
{
sign = true;
status = i;
}
}

if(sign == false)
{
str = str + ex;
}
cout<<"检测后:"<<str<<endl;

return 0;

}

下面是输出结果:

1.
请输入一个字符:a
检测前:abcdefghigk
字符串的长度是:11
检测后:abcdefghigk
请按任意键继续. . .

2.
请输入一个字符:m
检测前:abcdefghigk
字符串的长度是:11
检测后:abcdefghigkm
请按任意键继续. .