C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問(wèn)題
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問(wèn)題
主要實(shí)現(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ù)實(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 ;
}
測(cè)試匹配函數(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 ;//驗(yàn)證StrIndex1()
Sstring sub ;
StrAssign( str1 , "shfiodshfdghafhs" ) ;
StrAssign( sub , "dgh" ) ;
int i = StrIndex1( str1 , sub , 1 ) ;
printf( "%d\n" , i ) ;*/
/*Sstring str1 ;//驗(yàn)證StrIndex2()
Sstring sub ;
StrAssign( str1 , "shfiodshfdghafhs" ) ;
StrAssign( sub , "dgh" ) ;
int i = StrIndex2( str1 , sub , 1 ) ;
printf( "%d\n" , i ) ;*/
Sstring str1 ;//驗(yàn)證StrIndex3()
Sstring sub ;
StrAssign( str1 , "shfiodshfdghafhs" ) ;
StrAssign( sub , "dgh" ) ;
int i = StrIndex3( str1 , sub , 1 ) ;
printf( "%d\n" , i ) ;
return 0;
}
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C語(yǔ)言報(bào)錯(cuò)Use of Uninitialized Variable的原因及解決方案
Use of Uninitialized Variable是C語(yǔ)言中常見(jiàn)且危險(xiǎn)的錯(cuò)誤之一,它通常在程序試圖使用一個(gè)未初始化的變量時(shí)發(fā)生,本文將詳細(xì)介紹Use of Uninitialized Variable的產(chǎn)生原因,提供多種解決方案,并通過(guò)實(shí)例代碼演示如何有效避免和解決此類(lèi)錯(cuò)誤,需要的朋友可以參考下2024-06-06
opencv實(shí)現(xiàn)圖片與視頻中人臉檢測(cè)功能
這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)圖片與視頻中人臉檢測(cè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C語(yǔ)言格式輸出二進(jìn)制的2種方法總結(jié)
眾所周知C中以八進(jìn)制,十進(jìn)制和十六進(jìn)制都可以通過(guò)%o,%d和%x輕松實(shí)現(xiàn),然而唯獨(dú)沒(méi)有提供二進(jìn)制輸出的快速方式,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言格式輸出二進(jìn)制的2種方法,需要的朋友可以參考下2022-08-08
重構(gòu)-C++實(shí)現(xiàn)矩陣的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇重構(gòu)-C++實(shí)現(xiàn)矩陣的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
C++通過(guò)自定義函數(shù)找出一個(gè)整數(shù)數(shù)組中第二大數(shù)的方法
這篇文章主要介紹了C++通過(guò)自定義函數(shù)找出一個(gè)整數(shù)數(shù)組中第二大數(shù)的方法,涉及C++針對(duì)數(shù)組的遍歷操作相關(guān)技巧,需要的朋友可以參考下2015-06-06
C/C++ 中sizeof(''a'')對(duì)比詳細(xì)介紹
這篇文章主要介紹了C/C++ 中sizeof('a')的值對(duì)比詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02
C++?list容器merge算法的使用以及注意事項(xiàng)
這篇文章主要介紹了C++?list容器merge算法的使用以及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

