📄 字符串查找.cpp
字号:
//在主串s中查找字串t
#include<iostream.h>
int find(char s[],char t[]);
const int MAXLINE=256;
int main()
{
char source[MAXLINE],target[MAXLINE];
cout<<"Please input a string for searching:\n";
cin.getline(source,MAXLINE);
cout<<"Please input a string you want to find:\n";
cin.getline(target,MAXLINE);
int intPos=find(source,target);
if(intPos>=0)
cout<<"Finding it,The target string is at index "
<<intPos<<"of the source string\n";
else
cout<<"Not finding it\n";
return 0;
}
int find(char s[],char t[])
{
int i=0,j=0;
for(i=0;s[i]!='\0';i++)
{
if(t[0]==s[i]){
while(t[j]!='\0'&&s[i+j]!='\0'){
j++;
if(t[j]!=s[i+j])
break;
}
}
if(t[j]=='\0')
return i;
}
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -