顺序串c语言,数据结构c语言实现定长顺序串

头文件

#ifndef SLHEAD_H_INCLUDED

#define SLHEAD_H_INCLUDED

#include

#include

#include

#define MAXLEN 255

typedef char Sstring[MAXLEN + 1] ;

int StrAssign( Sstring str , char* ps ) ;

int StrCopy( Sstring str , Sstring s ) ;

int StrEmpty( Sstring str ) ;

int StrCompare( Sstring str1 , Sstring str2 ) ;

int StrLength( Sstring str ) ;

int StrClear( Sstring str ) ;

int StrConcat( Sstring  str1 , Sstring str2 ) ;

int StrSub( Sstring sub , Sstring str ,  int pos , int len ) ;

int StrIndex( Sstring str , Sstring sub , int pos ) ;

int StrReplace( Sstring str , Sstring source , Sstring dest ) ;

int StrInsert( Sstring str , Sstring st , int pos ) ;

int StrDelete( Sstring str , int pos , int len ) ;

int StrDestroy( Sstring str ) ;

#endif // SLHEAD_H_INCLUDED

函数实现

#include "slhead.h"

int StrAssign( Sstring str , char* ps )

{

int i = 0 ;

if( strlen( ps ) > MAXLEN )

{

printf( "ERROR!\n" ) ;

exit( 1 ) ;

}

while( ps[i] != '\0' )

{

str[i] = *( ps + i ) ;

i++ ;

}

str[i] = '\0' ;

return 0 ;

}

int StrCopy( Sstring str , Sstring s )

{

int i = 0 ;

while( ( str[i++] = s[i] ) != '\0' )

{

;

}

str[i] = '\0' ;

return 0 ;

}

int StrEmpty( Sstring str )

{

int i = 0 ;

if( str[i] == '\0')

{

return 1 ;

}

return 0 ;

}

int StrCompare( Sstring str1 , Sstring str2 )

{

int ret = 0 ;

char* p = str1 ;

char* q = str2 ;

while( !( ret = ( *(unsigned char* )p - *(unsigned char* )q ) ) && *q  )

{

p++ ;

q++ ;

}

if( ret < 0 )

{

return -1 ;

}

else if( ret == 0 )

{

return 0 ;

}

else

{

return 1 ;

}

}

int StrLength( Sstring str )

{

return strlen( str ) ;

}

int StrClear( Sstring str )

{

str[0] = '\0' ;

return 0 ;

}

int StrConcat( Sstring  str1 , Sstring str2 )

{

if( strlen( str1 ) + strlen( str2 ) > MAXLEN )

{

printf( "ERROR!\n" ) ;

exit( 1 ) ;

}

char* p = str1 ;

char* q = str2 ;

while( *p )

{

p++ ;

}

while( ( *p++ = *q++ ) != '\0' )

{

;

}

return 0 ;

}

int StrSub( Sstring sub , Sstring str ,  int pos , int len )

{

if( ( pos < 0 ) || ( pos > strlen( str ) ) || ( len < 0 ) || ( len > strlen( str ) - pos + 1 ) )

{

printf( "ERROR!\n" ) ;

exit( 1 ) ;

}

int i = pos ;

char* p = sub ;

while( i < pos +len )

{

*p++ = str[i - 1] ;

i++ ;

}

*p = '\0' ;

return 0 ;

}

int StrIndex( Sstring str , Sstring sub , int pos )

{

if( ( pos < 1 ) || ( pos > strlen( str) - strlen( sub ) + 1 ) )

{

printf( "ERROR!\n" ) ;

exit( 1 ) ;

}

int i = pos - 1 ;

int j = 0 ;

while( ( j < strlen( sub ) ) && ( i < strlen( str ) ) )

{

if( str[i] != sub[j] )

{

i = i - j + 1 ;

j = 0 ;

}

else

{

i++ ;

j++ ;

}

}

if( sub[j] == '\0')

{

return i - j  + 1 ;

}

return 0 ;

}

int StrInsert( Sstring str , Sstring st , int pos )

{

if( ( strlen( st ) == 0 ) || ( pos < 1 ) || ( pos > strlen( str ) + 1 ) )

{

printf( "ERROR1!\n" ) ;

exit( 1 ) ;

}

if( strlen( str ) + strlen( st ) > MAXLEN )

{

printf( "ERROR2!\n" ) ;

exit( 1 ) ;

}

int i = strlen( str ) + strlen( st ) ;

int j = strlen( str ) ;

while( j >= pos - 1 )

{

str[i--] = str[j--] ;

}

i = pos - 1 ;

j= 0 ;

while( j < strlen( st ) )

{

str[i++] = st[j++] ;

}

return 0 ;

}

int StrDelete( Sstring str , int pos , int len )

{

int i = 0 ;

while( ( pos - 1 + len + i ) <= strlen( str ) )

{

str[pos - 1 + i] = str[pos - 1 + len + i] ;

i++ ;

}

return 0 ;

}

int StrReplace( Sstring str , Sstring source , Sstring dest )

{

int pos = 1 ;

int flag = 0 ;

while( str[pos] != '\0' )

{

pos = StrIndex( str , source , pos ) ;

if( pos == 0 )

{

if( flag )

{

printf( "success to replace !\n" ) ;

return 0 ;

}

else

{

printf( "fail to replace !\n" ) ;

return 1 ;

}

}

StrDelete( str , pos , strlen(source) ) ;

StrInsert( str , dest , pos ) ;

pos = pos + strlen( dest ) + 1 ;

flag = 1 ;

}

return 0 ;

}

主函数,部分函数的测试

#include "slhead.h" int main() {     /*Sstring str = "abc" ;     Sstring str1 = "ab" ;     StrConcat( str , str1 ) ;     printf( "%s\n" , str ) ;*/     /*Sstring str = "abcdsfasd" ;     Sstring str1 = "ab" ;     StrSub( str1 , str , 3 , 4 ) ;     printf( "%s\n" , str1 ) ;*/     /*Sstring str = "abcdsfasd" ;     Sstring str1 = "as" ;     int pos = StrIndex( str , str1 , 1 ) ;     printf( "%d\n" , pos ) ;*/     return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值