当前位置:编程文档 >> C/C++ >> C++实现字符串查找
首页

C++实现字符串查找

所属类别:C/C++
推荐指数:★★☆
文档人气:886
本周人气:14
发布日期:2007-7-2

#include <iostream>
#include <string>

using namespace std;
int findchar(const char *s,const char *t);

int main(int argc, char* argv[])
{
string s;
string t;
cout<<"输入第一个字符串(以输入#结束): \b";
getline(cin,s,'#');
cout<<"输入第一个字符串(以输入#结束): \b";
getline(cin,t,'#');

int pos = findchar(s.c_str(),t.c_str());

if (pos == 0)
{
cout<<"查找失败!"<<endl;
}
else
{
cout<<s[pos]<<"出现在S的最右边第"<<pos<<"个"<<endl;
}
return EXIT_SUCCESS;
}
int findchar(const char *s,const char *t)
{
int pos = 0;
int lenS = strlen(s);
int lenT = strlen(t);
for (int i=0; i<lenT ; ++i)
{
for (int j=lenS; j>=0; --j)
{
if (s[j] == t[i])
{
pos = lenS-j;
return pos;
break;
}
}
}
return pos;
}

文档说明:

     

相关文档


读取评论列表……