標頭檔案hstring.h:
/*hstring.h*/
#ifndef hstring_h_included
#define hstring_h_included
#include #include #include #define defaultsize 30
typedef struct
hstring;
#endif /*hstring_h_included*/
原始檔hstring.c:
/*hstring.c*/
#include "hstring.h"
hstring *initstr ( hstring *s )/*串初始化*/
void createstr ( hstring *s, char *init )/*從字元陣列init構造串s*/
void copystr ( hstring *s, hstring *t )/*把串t複製給串s*/
hstring *substr ( hstring *s, int pos, int len )/*在串s中連續取從pos開始的len個字元,
構成子串返回。若提取失敗則函式返回null*/
else
return tmp;
}void concat ( hstring *s, hstring *t )
else
}char getchar ( hstring *s, int i )/*提取串s的第i個字元*/
return s->ch[i];
}void printstr ( hstring *s )
測試原始檔test.c:
/*test.c*/
#include "hstring.h"
#define defsize 30
/*hefeiuniversityoftechnology
*/int main ( )
; char b[defsize] = ;
hstring *s1 = ( hstring * )malloc( sizeof(hstring) ), *s2 = ( hstring * )malloc( sizeof(hstring) ), *s3 = ( hstring * )malloc( sizeof(hstring) ), *s4 = ( hstring * )malloc( sizeof(hstring) );
s3 = initstr ( s3 );
s4 = initstr ( s4 );
printf ( "createstring ( s1, a )\n" );
createstr ( s1, a );
printstr ( s1 );
printf ( "createstring ( s2, b )\n" );
createstr ( s2, b);
printstr ( s2 );
printf ( "copy ( s3, s2 )\n" );
copystr ( s3, s2 );
printstr ( s3 );
printf ( "s4 = substr( s3, 5, 3 )\n" );
s4 = substr( s3, 5, 3 );
printstr ( s4 );
printf ( "initstring ( s4 )\n" );
initstr ( s4 );
printstr ( s4 );
printf ( "s4 = substr ( s3, 8, 5 )\n" );
s4 = substr ( s3, 8, 5 );
printstr ( s3 );
printstr ( s4 );
printf ( "concat ( s1, s4 )\n" );
concat ( s1, s4 );
printstr ( s1 );
printf ( "%c\n", getchar ( s1, 5 ) );
return 0;
}
程式執行截圖:
刁肥宅手筆 純C語言實現鏈式佇列的相關操作
先上圖,以圖服人 上 標頭檔案linkqueue.h linkqueue.h ifndef linkqueue h included define linkqueue h included include include include include include include typedef...
C語言實現串的堆分配儲存
heap string.h 串的堆分配儲存實現,用這種實現方法的好處是,能夠動態的給 串分配記憶體空間,而順序串不能 created on 2011 9 7 author root define elemtype char define true 1 define false 0 typedef s...
用C語言實現多項式合併(函式宣告和主函式)
case1 冪遞增 多項式l1 5 2x2 4x3 6x5 多項式l2 x 3x2 8x4 7x5 x8 l1 l2 5 x 5x2 4x3 8x4 13x5 x8 case2 冪任意 多項式l1 3x3 4x2 5 x6 2x5 6x4 多項式l2 x 5x3 2x2 5x6 3x5 6 l1 l...