資料結構c語言實現定長順序串

2021-06-07 21:40:21 字數 2667 閱讀 8357

標頭檔案

#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 )

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

str[i] = '\0' ;

return 0 ;

}int strcopy( sstring str , sstring s )

str[i] = '\0' ;

return 0 ;

}int strempty( sstring str )

return 0 ;

}int strcompare( sstring str1 , sstring str2 )

if( ret < 0 )

else if( ret == 0 )

else

}int strlength( sstring str )

int strclear( sstring str )

int strconcat( sstring  str1 , sstring str2 )

char* p = str1 ;

char* q = str2 ;

while( *p )

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

return 0 ;

}int strsub( sstring sub , sstring str ,  int pos , int len )

int i = pos ;

char* p = sub ;

while( i < pos +len )

*p = '\0' ;

return 0 ;

}int strindex( sstring str , sstring sub , int pos )

int i = pos - 1 ;

int j = 0 ;

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

else

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

return 0 ;

}int strinsert( sstring str , sstring st , int pos )

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

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

int j = strlen( str ) ;

while( j >= pos - 1 )

i = pos - 1 ;

j= 0 ;

while( j < strlen( st ) )

return 0 ;

}int strdelete( sstring str , int pos , int len )

return 0 ;

}int strreplace( sstring str , sstring source , sstring dest )

else

}strdelete( str , pos , strlen(source) ) ;

strinsert( str , dest , pos ) ;

pos = pos + strlen( dest ) + 1 ;

flag = 1 ;

}return 0 ;

}主函式,部分函式的測試

#include "slhead.h"

int main()

C語言 串定長順序儲存實現 資料結構十二

1.資料型別定義 在 中為了清楚的表示一些錯誤和函式執行狀態,我們預先定義一些變數來表示這些狀態。在head.h標頭檔案中有如下定義 定義資料結構中要用到的一些變數和型別 ifndef head h define head h include include include include defi...

串的定長順序儲存C語言實現

串 字串 是由0個或多個字元組成的有限序列。0個字元時稱為空串。由乙個或多個空格組成的串 稱為空格串。串中字元的數目n稱為串的長度 串中任意個連續的字元組成的子串行稱為該串的字串 包含字串的串相應的稱為主串 通常稱字元在序列中的序號稱為該字元在串中的位置。字串在主串中的位置則以字串的第乙個字元在主串...

定長順序串 C語言

題目 用定長順序串編寫下列演算法 1.將順序串r中所有值為ch1的字元轉換成ch2的字元。2.將順序串r中所有字元按照相反次序仍存放在r中。3.從順序串r中刪除其值等於ch的所有字元。4.從順序串r1中第index個字元起求首次與串r2相同的子串的起始位置。5.從順序串r中刪除所有與串r1相同的子串...