C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問題
主要實(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;
}
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C語(yǔ)言中初始、增加和刪除進(jìn)程信號(hào)的操作方法簡(jiǎn)介
這篇文章主要介紹了C語(yǔ)言中初始、增加和刪除進(jìn)程信號(hào)的操作方法簡(jiǎn)介,分別是sigemptyset函數(shù)、sigaddset函數(shù)和sigdelset函數(shù)的用法,需要的朋友可以參考下2015-09-09
Qt利用QDrag實(shí)現(xiàn)拖拽拼圖功能詳解
QDrag類為MIME-based拖拽數(shù)據(jù)轉(zhuǎn)換提供支持。本文為大家主要介紹如何利用QDrag類實(shí)現(xiàn)拖拽拼圖功能。左邊是打散的圖,拖動(dòng)到右邊進(jìn)行復(fù)現(xiàn),此外程序還支持手動(dòng)拖入原圖片,感興趣的可以了解一下2022-07-07
C語(yǔ)言用函數(shù)實(shí)現(xiàn)反彈球消磚塊
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言用函數(shù)實(shí)現(xiàn)反彈球消磚塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
c語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)之字符串
這篇文章主要介紹了c語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)之字符串的相關(guān)資料,需要的朋友可以參考下2017-05-05
基于C語(yǔ)言實(shí)現(xiàn)關(guān)機(jī)小游戲的示例代碼
關(guān)機(jī)會(huì)寫吧!猜數(shù)字會(huì)寫吧!本文將結(jié)合這兩個(gè)功能,用C語(yǔ)言編寫一個(gè)關(guān)機(jī)惡搞小游戲(最好的朋友轉(zhuǎn)瞬即逝),只要猜對(duì)了,1分鐘后執(zhí)行關(guān)機(jī),除非輸入“我是豬”,但是輸完后,1分鐘后還是會(huì)執(zhí)行關(guān)機(jī),該保存保存,感興趣的可以嘗試一下2022-07-07
C語(yǔ)言強(qiáng)制類型轉(zhuǎn)換規(guī)則實(shí)例詳解
強(qiáng)制類型轉(zhuǎn)換是把變量從一種類型轉(zhuǎn)換為另一種數(shù)據(jù)類型,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言強(qiáng)制類型轉(zhuǎn)換的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
C++ OpenCV實(shí)現(xiàn)白平衡之灰度世界算法
灰度世界算法是白平衡各種算法中最基本的一種。本文將利用C++和OpenCV實(shí)現(xiàn)白平衡中的灰度世界算法,文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05

