C語言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題
C語言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題
主要實現(xiàn)了三種字符串的模式匹配,主要包括字符串子操作的集合,字符串指針回溯,和KMP算法
頭文件
#ifndef INDEXHEAD_H_INCLUDED #define INDEXHEAD_H_INCLUDED #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 255 typedef char Sstring[MAXLEN + 1] ; int StrAssign( Sstring str , char* ps ) ; int StrLength( Sstring str ) ; int StrPrint( Sstring str ) ; int StrCompare( Sstring str1 , Sstring str2 ) ; int StrSub( Sstring sub , Sstring str , int pos , int length ) ; int StrIndex1( Sstring str , Sstring sub , int pos ) ; int StrIndex2( Sstring str , Sstring sub , int pos ) ; int StrIndex3( Sstring str , Sstring sub , int pos ) ; int GetNext( Sstring str , int next[] ) ; #endif // INDEXHEAD_H_INCLUDED
函數(shù)實現(xiàn)
#include "indexhead.h" int StrAssign( Sstring str , char* ps ) { int i = 0 ; if( strlen( ps ) > MAXLEN ) { printf( "ERROR!\n" ) ; exit( 1 ) ; } str[i++] = strlen( ps ) ; while( i <= strlen( ps ) ) { str[i] = *( ps + i - 1 ) ; i++ ; } return 0 ; } int StrPrint( Sstring str ) { int i = 1 ; while( i <= str[0] ) { printf( "%c" , str[i++] ) ; } printf( "\n" ) ; return 0 ; } int StrLength( Sstring str ) { return str[0] ; } int StrCompare( Sstring str1 , Sstring str2 ) { int i = 1 ; int ret = 0 ; while( i <= str1[0] && i <= str2[0] ) { ret = ( unsigned char )str1[i] - ( unsigned char )str2[i] ; if( ret < 0 ) { return -1 ; } else if( ret > 0 ) { return 1 ; } else { i++ ; } } if( i <= str1[0] ) { return -1 ; } else if( i <= str2[0] ) { return 1 ; } else { return 0 ; } } int StrSub( Sstring sub , Sstring str , int pos , int length ) { if( pos < 1 || pos > str[0] || length < 0 || length > str[0] - pos + 1 ) { printf( "ERROR!\n" ) ; exit( 1 ) ; } int i = 1 ; sub[0] = length ; while( i <= length ) { sub[i] = str[pos + i - 1] ; i++ ; } return 0 ; } int StrIndex1( Sstring str , Sstring sub , int pos ) { pos = 1 ; Sstring stemp ; while( pos <= str[0] - sub[0] + 1 ) { StrSub( stemp , str , pos , sub[0] ) ; if( !StrCompare( stemp , sub ) ) { return pos ; } pos++ ; } return 0 ; } int StrIndex2( Sstring str , Sstring sub , int pos ) { if( pos < 1 || pos > str[0] - sub[0] + 1 ) { printf( "ERROR!\n" ) ; exit( 1 ) ; } int i = pos ; int j = 1 ; while( ( i <= str[0] - sub[0] + 1 ) && ( j <= sub[0] ) ) { if( str[i] == sub[j] ) { i++ ; j++ ; } else { i = i - j + 2 ; j = 1 ; } } if( j > sub[0] ) { return i - sub[0] ; } return 0 ; } int GetNext( Sstring str , int next[] ) { int i = 1 ; next[1] = 0 ; int j = 0 ; while( i < str[0] ) { if( j== 0 || str[i] == str[j] ) { ++i ; ++j ; next[i] = j ; } else { j = next[j] ; } } return 0 ; } int StrIndex3( Sstring str , Sstring sub , int pos ) { int i = pos ; int j = 1 ; int next[ sub[0] ] ; GetNext( sub , next ) ; while( i <= str[0] && j <= sub[0] ) { if( j == 0 || str[i] == sub[j] ) { ++i ; ++j ; } else { j = next[j] ; } } if( j > sub[0] ) { return i - sub[0] ; } return 0 ; }
測試匹配函數(shù)
#include "indexhead.h" int main() { /*Sstring str ; Sstring str1 ; char* p = "hello" ; StrAssign( str , p ) ; StrAssign( str1 , "ahello" ) ; StrPrint( str ) ; int i = StrLength( str ) ; printf( "%d\n" , i ) ; int j = StrCompare( str , str1 ) ; printf( "%d\n" , j ) ; StrSub( str , str1 , 3 , 4 ) ; StrPrint( str ) ;*/ /*Sstring str1 ;//驗證StrIndex1() Sstring sub ; StrAssign( str1 , "shfiodshfdghafhs" ) ; StrAssign( sub , "dgh" ) ; int i = StrIndex1( str1 , sub , 1 ) ; printf( "%d\n" , i ) ;*/ /*Sstring str1 ;//驗證StrIndex2() Sstring sub ; StrAssign( str1 , "shfiodshfdghafhs" ) ; StrAssign( sub , "dgh" ) ; int i = StrIndex2( str1 , sub , 1 ) ; printf( "%d\n" , i ) ;*/ Sstring str1 ;//驗證StrIndex3() Sstring sub ; StrAssign( str1 , "shfiodshfdghafhs" ) ; StrAssign( sub , "dgh" ) ; int i = StrIndex3( str1 , sub , 1 ) ; printf( "%d\n" , i ) ; return 0; }
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C語言
下一篇:C 讀取ini文件的實例詳解
本文標(biāo)題:C語言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1059.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery