字符串匹配

来源:互联网 发布:中国出境游数据 编辑:程序博客网 时间:2024/06/07 19:46
#include <stdio.h>#include <assert.h>#include <string.h>#define MAX 1000int getline(char line[], int max){int ch;int i = 0;while (max > 0 &&(ch = getchar()) != EOF && ch != '\n'){line[i] = ch;i++;max--;}if (ch == '\n')line[i++] = '\n';line[i] = '\0';if (i > 0)return 1;elsereturn 0;}int match(char line[],char *mat){assert(line);assert(mat);int i=0, j=0, k=0;for (i = 0; i < strlen(line); i++){for (k = i,j = 0; j < strlen(mat); j++,k++){if (line[k] != *(mat + j))break;}if (*(mat+j) == '\0' && k>0)return 1;}return 0;}int main(){char line[MAX];char *mat = "ould";while (getline(line,MAX)){if (match(line,mat))printf("%s", line);}return 0;}


本代码主要实现字符串匹配问题。

输入一段文字(英文),然后输入想查找的部分,即可输出含有相同字符串的语句。

0 0
原创粉丝点击