C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之動(dòng)態(tài)分配實(shí)現(xiàn)串
華容網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,華容網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為華容數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的華容做網(wǎng)站的公司定做!
說(shuō)明:堆分配存儲(chǔ)實(shí)現(xiàn)串時(shí),串并不是以'\0‘, 而是用數(shù)據(jù)項(xiàng)int length來(lái)表示的,所以和傳統(tǒng)的c語(yǔ)言操作字符串有所不同。
頭文件
#ifndef PILEHEAD_H_INCLUDED #define PILEHEAD_H_INCLUDED #include#include typedef struct { char* ch ; int len ; }HString ; int StrAssign( HString* pstr , char* pch ) ; int StrLength( HString* pstr ) ; int StrCompaer( HString* pstr1 , HString* pstr2 ) ; int StrClear( HString* pstr ) ; int StrConcat( HString* pstr , HString str1 , HString str2 ) ; int StrSub( HString* sub , HString pstr , int pos , int length ) ; int StrPrint( HString* pstr ) ; #endif // PILEHEAD_H_INCLUDED
函數(shù)實(shí)現(xiàn)
#include "pilehead.h" int StrAssign( HString* pstr , char* pch ) { if( !pstr->len ) { free( pstr->ch ) ; } int i ; for( i = 0 ; pch[i] != '\0' ; i++ ) { ; } if( i == 0 ) { pstr->ch = NULL ; pstr->len = 0 ; } else { pstr->ch = ( char* )malloc( i * sizeof( char ) ) ; pstr->len = i ; } i = 0 ; while( i < pstr->len ) { pstr->ch[i] = pch[i] ; i++ ; } return 0 ; } int StrPrint( HString* pstr ) { int i = 0 ; while( i < pstr->len ) { printf( "%c" , pstr->ch[i] ) ; i++ ; } printf( "\n" ) ; return 0 ; } int StrLength( HString* pstr ) { return pstr->len ; } int StrCompaer( HString* pstr1 , HString* pstr2 ) { int i = 0 ; int ret = 0 ; while( i < pstr1->len && ilen ) { ret = pstr1->ch[i] - pstr2->ch[i] ; if( ret < 0 ) { return -1 ; } else if( ret > 0 ) { return 1 ; } else { i++ ; } } int diff = pstr1->len - pstr2->len ; if( diff < 0 ) { return -1 ; } else if( diff == 0 ) { return 0 ; } return 1 ; } int StrClear( HString* pstr ) { if( pstr->ch ) { free( pstr->ch ) ; pstr->ch = NULL ; } pstr->len = 0 ; return 0 ; } int StrConcat( HString* pstr , HString str1 , HString str2 ) { StrClear( pstr ) ; StrAssign( pstr , str1.ch ) ; int len = str1.len ; int len2 = 0 ; while( len2 < str2.len ) { pstr->ch[len++] = str2.ch[len2++] ; } pstr->len = str1.len + str2.len ; return 0 ; } int StrSub( HString* sub , HString pstr , int pos , int length ) { if( pos < 1 || pos > pstr.len || length < 1 || length > pstr.len - pos + 1 ) { printf( "ERROR!\n" ) ; exit( 1 ) ; } StrClear( sub ) ; sub->ch = ( char* )malloc( length * sizeof( char ) ) ; sub->len = length ; int i = 0 ; while( i < length ) { sub->ch[i] = pstr.ch[pos - 1 + i] ; i++ ; } return 0 ; }
測(cè)試函數(shù)
#include "pilehead.h" int main() { HString str ; HString str1 ; HString str2 ; char* pch = "abcd" ; StrAssign( &str , pch ) ; StrAssign( &str1 , "abcd" ) ; StrAssign( &str2 , "fhasdifhos" ) ; StrPrint( &str ) ; StrPrint( &str1 ) ; /*StrClear( &str ) ; StrPrint( &str ) ; int i = StrCompaer( &str , &str1 ) ; printf( "%d" , i ) ;*/ StrConcat( &str , str1 , str2 ) ; StrPrint( &str ) ; HString s ; StrSub( &s ,str , 4 , 5 ) ; StrPrint( &s ) ; return 0; }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!