DS:串的简单匹配

来源:互联网 发布:淘宝网宝贝主图尺寸 编辑:程序博客网 时间:2024/06/06 00:07

首次匹配成功,返回待匹配的字符串 data 在 字符串 str  的首个元素位置;否则,返回0:

// STACK.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<time.h>#include<string.h>int compare(char*str,char*data);int main(){char str[30]="sdfhjffdkkkdkdhkdhslklk",data[10]="fdkkkd";puts(str);puts(data);   printf("\nsince %dth member(the string begin with 0)\n",compare(str,data));return 0;}int compare(char*str,char*data){int j=0;for(int i=0;str[i]!='\0';i++){if(str[i]==data[j]){for(j=1;data[j]!='\0';j++){if(str[i+j]!=data[j]){j=0;break;}}if(data[j]=='\0')return i;}}return 0;}


原创粉丝点击