实现一个挺高级的字符匹配算法:给一串很长字符串,要求找到符合要求的字符串,例如目的串:1231******3***2 ,12*****3这些都要找出来.扩展一问,匹配时要考虑顺序,如132是不匹配的,但是1*****2*3是匹配的。
这个扩展问做起来应该是比原来的复杂的。循环很多,容易出错,调了好久才过。
扩展问题代码:
- /*
- * =====================================================================================
- *
- * Filename: advstrindex.c
- *
- * Description:
- *
- * Version: 1.0
- * Created: 10/03/2012 03:00:22 PM
- * Revision: none
- * Compiler: gcc
- *
- * Author: YOUR NAME (),
- * Organization:
- *
- * =====================================================================================
- */
- #include <stdlib.h>
- #include <stdio.h>
- void advsearch(char *input, char*pattern){
- char* istart = input;
- char* pstart = pattern;
- while(1){
- char *rstart = NULL;
- char *rend = NULL;
- while( *istart != *pstart && *istart){
- istart++;
- }
- rstart = istart;
- istart++;
- pstart++;
- while(*istart){
- if(*istart == *pstart) {
- pstart++;
- }
- if(*pstart == '\0'){
- rend = istart;
- break;
- }
- istart++;
- }
- if(*rstart == '\0'|| !rend || !rstart) return;
- istart = rstart+1;
- pstart = pattern;
- while(*rstart && *rend && rstart<=rend){
- printf ( "%c", *rstart );
- rstart++;
- }
- printf ( "\n" );
-
- }
- }
- /*
- * === FUNCTION ======================================================================
- * Name: main
- * Description:
- * =====================================================================================
- */
- int
- main ( int argc, char *argv[] )
- {
- char input[] = "eacbabca";
- char pattern[] = "abc";
- advsearch(input, pattern);
- return EXIT_SUCCESS;
- } /* ---------- end of function main ---------- */
阅读(166) | 评论(0) | 转发(0) |