1. 程式人生 > >C語言字串匹配函式

C語言字串匹配函式

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <assert.h>
  5 #include <time.h>
  6 
  7 /*
  8 pattern:
  9 pos:
 10 */
 11 
 12 static int badShift[256];
 13 
 14 
 15 static int goodPostfixLastPos(const char *pattern,int
pos) 16 { 17 #define _break(flag) if(flag){ break;} 18 19 int flag = 0; 20 int len = strlen(pattern); 21 int postFix_len = len - pos; 22 int postFix_position = pos; 23 int initStart = pos - postFix_len; 24 int last_start = 0; 25 while(postFix_len) 26 {
27 last_start = (postFix_position == pos) ?initStart:0; 28 int postFix_start = postFix_position; 29 for(;last_start>=0 && postFix_start<len;last_start++,postFix_start++) 30 { 31 flag = (pattern[last_start] == pattern[postFix_start]); 32
_break(!flag); 33 34 } 35 36 _break(flag); 37 if(initStart >= 0) 38 { 39 initStart--; 40 } 41 else 42 { 43 postFix_position++; 44 postFix_len--; 45 } 46 } 47 48 return flag?last_start-1:-1; 49 } 50 51 static int *calc_goodPostfixShift(const char *pattern,int *goodShift) 52 { 53 int len = strlen(pattern); 54 for(int i=0;i<len;i++) 55 { 56 goodShift[i] = len - goodPostfixLastPos(pattern,i) - 1; 57 } 58 59 return goodShift; 60 } 61 62 static int *clac_badcharShift(const char *ptrn) 63 { 64 int i; 65 int pLen = strlen(ptrn); 66 67 for(i = 0; i < 256; i++) 68 { 69 *(badShift+i) = pLen; 70 } 71 72 while(pLen != 0) 73 { 74 *(badShift+(unsigned char)*ptrn++) = --pLen; 75 } 76 77 return badShift; 78 } 79 80 int BMSearch(const char *str,const char *pattern) 81 { 82 83 int goodShift[strlen(pattern)]; 84 int len1 = strlen(str); 85 int len2 = strlen(pattern); 86 87 clac_badcharShift(pattern); 88 calc_goodPostfixShift(pattern,goodShift); 89 for(int i=len2 - 1;i<len1;) 90 { 91 int start = i; 92 int pos_pattern = len2 - 1; 93 for(;pos_pattern>=0;pos_pattern--,start--) 94 { 95 if(str[start] != pattern[pos_pattern]) 96 { 97 break; 98 } 99 } 100 if(pos_pattern < 0) 101 { 102 return start + 1; 103 } 104 105 if(pos_pattern == (len2 - 1)) 106 { 107 i += badShift[str[start]]; 108 } 109 else 110 { 111 i += goodShift[pos_pattern + 1]; 112 } 113 } 114 115 return -1; 116 }

相關推薦

C語言字串匹配函式

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <assert.h> 5 #include <time.h>

(C/C++學習)14.C語言字串處理函式(二)

說明:上節著重解釋了字串處理的庫函式處理,這節將針對一些常用的需求,進行非庫函式的處理。 一.去除某一個字串中的某個字元 1.去除字串右邊的空格 1 void trimStrRightSpace(char * str) 2 { 3 while(*str) 4 s

14. C語言 -- 字串處理函式

本部落格主要內容為 “小甲魚” 視訊課程《帶你學C帶你飛》【第一季】 學習筆記,文章的主題內容均來自該課程,在這裡僅作學習交流。在文章中可能出現一些錯誤或者不準確的地方,如發現請積極指出,十分感謝。 也歡迎大家一起討論交流,如果你覺得這篇文章對你有所幫助,記得評論、點贊哦 ~(。

常用的c語言字串處理函式

1.獲取一個字串 char *p = fgets(buf, size, stdin);   2.字串查詢     ret = strstr(cmd, "test");     if(ret != NULL)    

c語言字串簡單函式簡單應用

    //記憶字串的函式      //1.strcpy賦值函式的用法;      /*char a[50]="hello world";     char b[50];     strcpy(b,a);     printf("%s",b);*/          //2

C語言字串處理函式

1、puts(字串陣列) ,gets(字串陣列) 2、strcat(字串陣列1,字串陣列2) 把字串陣列2接到字串陣列1中。 3、strcpy(字串陣列1,字串陣列2) 把字串陣列2複製到字串陣列1中。 strncpy(字串陣列1,字串陣列2,n),將字串2中前n個

C語言字串分割函式split實現

#include <stdlib.h> #include <string.h> #include <stdio.h> /* 用delimiter中的每一個分隔符分割str字串,這並不會改變str中的字元,然後返回一個字串陣列. 字串陣列中的字串有可能是以'\0'開頭的空串

C 語言字串分割函式 p = strtok(NULL, " ");

原始碼: #include <stdio.h> #include<string.h> int main() { char str[] = "經度:111°11’11'' 緯度: 30°30'30''"; char *p; char a[]=" "

C語言字串操作函式

C語言中(部分)字串操作函式簡介 strlen(p); // 計算指定的字串p的長度,不包括結束字元'\0' strcpy(p, p1); // 複製字串  strncpy(p, p1, n); // 複製指定長度字串  strcat(p, p1);

C語言字串擷取函式strtok和strtok_r

       在看原始碼的時候需要將一段並排的IPs轉化成為一系列的IP,將"10.0.0.1;10.0.0.2;10.0.0.3;10.0.0.4;10.0.0.5"轉換成為單獨的"10.0.0.1

C語言字串查詢函式

轉自:blog.csdn.net/minpro //此函式的功能是在一個長字串中,查詢子串 //僅保留,以便使用,請勿見笑! /***********************************************************************

C語言字串操作函式總結

Part 1: Copying 1、函式名: strcpy 用法: char * strcpy ( char * destination, const char * source ); 功能: 串拷貝(

C++ primer plus書之--C++函式C語言字串, 結構體, string

函式和C風格字串 要將C風格字串作為引數傳遞給函式, 表示字串的方式有三種: 1.char陣列 2.用""擴起來的字串常量 3.被設定為字串地址的char指標 來看一個例子: // c風格字串例子 #include "iostream" using namespace std

C語言字串處理基礎函式(一)

1.strlen()函式 功能:函式返回字串str 的長度( 即空值結束符之前字元數目,不包括控制結束符)。 語法: #include <string.h> size_t strlen( char *str ); 例子: #include <stdio

C語言字串讀入函式筆記

gets(str)函式和scanf("%s",str)區別:   轉自:https://zhidao.baidu.com/question/290403568.html   二者都是從終端讀入字串。 功能為:  1、 gets功能為讀入一行,並將換行符轉換為字串結束符。  2、 scanf(

C語言實現一個函式,可以左旋字串中的k個字元

// 實現一個函式,可以左旋字串中的k個字元   例如:                 //ABCD左旋一個字元得到BCDA                 //ABCD左旋兩個字元得到CDAB    解題思路:                   1> 先理思

C語言字串函式歸納。

首先需要知道在c語言中本身並沒有字串型別,字串通常放在常量字串中或者字元陣列中 1.實現strlen 首先strlen函式的作用是求字串長度的。'\0’是它的結束標誌,strlen函式返回的是在字串中‘\0’前面出現的字元個數,strlen函式的返回值為size_t,是無

C語言字串函式總結:模擬實現常用的字串函式(strlen、strcpy、strcmp........)

總結:模擬實現常用的字串類函式(strlen、strcpy、strcmp……..) 1. strlen 2. strcpy 3. strcat 4. strstr 5. strchr 6. strcmp 7. memcpy 8. m

C語言——字串函式

C語言——字串函式 宗旨:技術的學習是有限的,分享的精神是無限的。 常用字串操作函式的實現:註釋部分你們注意一下,我沒加/,加了/就看不到註釋了。 ************************************************* *功能:實現字串的拷貝

C語言字串標頭檔案string.h中的strlen,strcat,strcpy,strncpy,strcmp,strlwr,strupr函式

前言 本博文內容基於VC6.0++, 2.本博文主要介紹包含在string.h中的幾個字串相關的函式; strlen(字串陣列名) 字串有效長度函式; 返回值:整型 功能:用來測量一個字串的有效長度(所謂有效,就是從字串中的第一個字元開始,一