字符串查找.cpp

来自「包含冒泡排序算法函数模板和数据结构中的字符串查找例子及解线性方程组的3个典型算法」· C++ 代码 · 共 37 行

CPP
37
字号
//在主串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 + =
减小字号Ctrl + -
显示快捷键?