#include<iostream>
using namespace std;
bool getsubstr(char *str1, char *str2, int loc[ ]) // 查找字符串子序列函数
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len2 > len1)
return false;
for(int i = 0, j = 0; i < len1; i++)
{
if( str1[i] == str2[j] && j < len2)
{
loc[j] = i; j++;
}
if(j > len2)
return false;
}
return true;
}
int main()
{
char s1[20] = "xupengxjwhelloworld"; //主串
char s2[20] = "pengxjw"; // 模式串
int loc[30]; // 记录模式串中个符号在主串中的位置
cout<<"yes or no ? "<<getsubstr(s1,s2,loc)<<endl;
for(int j = 0; j < 7; j++)
printf("%c",s1[loc[j]]);
printf("\n");
return 0;
}
字符串系列 小实验。