串是一種特殊的線性表,它的數據對象是字符集合。
一些功能可以參考string.h裏的函數。
SeqString.h
/* Name: SeqString.h Version: 0.10 Author: dir Date: 17-06-09 23:29 Description: */ #ifndef __SEQSTRING__H__ #define __SEQSTRING__H__ #include <stdlib.h> #include <stdio.h> #define MaxLen 256 typedef struct { char str[MaxLen]; int curlen; }SeqString; // 串初始化 void InitStr(SeqString *s) { memset(s->str, 0, sizeof(s->str)); s->curlen = 0; } // 串賦值 void Strassign(SeqString *s, char ch[]) { int pos = 0; while (ch[pos] != '/0') { s->str[pos] = ch[pos]; pos++; } s->str[pos] = '/0'; s->curlen = pos; } // 求串長 int StrLen(SeqString *s) { return s->curlen; } // 串複製 SeqString* StrCpy(SeqString *s, SeqString *t) { int pos; // 先判斷要複製的串是否有內容 if (t->curlen == 0) { printf("String is not initialize!/n"); exit(1); } for (pos = 0; pos <= t->curlen; pos++) { s->str[pos] = t->str[pos]; } s->curlen = t->curlen; return s; } // 串連接 SeqString* StrCat(SeqString *s, SeqString *t) { int pos; // 先判斷要連接的串是否有內容 if (t->curlen == 0) { printf("String is not initialize!/n"); exit(1); } if (s->curlen + t->curlen >= MaxLen) { printf("String length is not enough!/n"); exit(1); } for (pos = 0; pos <= t->curlen; pos++) { s->str[s->curlen+pos] = t->str[pos]; } s->curlen += t->curlen; return s; } // 求子串 SeqString* StrSub(SeqString *s, int iStart, int iEnd, SeqString *t) { int pos, len = 0; if (s->curlen == 0) { printf("String is not initialize!/n"); exit(1); } for (pos = iStart - 1; pos < iEnd; pos++) { t->str[len] = s->str[pos]; len++; } t->str[len] = '/0'; t->curlen = len; return t; } // 插入串 SeqString* StrInsert(SeqString *s, int pos, SeqString *t) { int i; if (s->curlen == 0) { printf("String is not initialize!/n"); exit(1); } // 判斷插入位置 if (pos < 1 || pos > s->curlen) { printf("Insert position error!/n"); exit(1); } // 先移動插入點之後的字符 for (i = s->curlen; i >= pos; i--) { s->str[i+t->curlen] = s->str[i]; } // 插入t串 for (i = pos; i < t->curlen + pos; i++) { s->str[i] = t->str[i-pos]; s->curlen++; } return s; } // 串刪除 SeqString* StrDel(SeqString *s, int pos, int len) { int i; if (s->curlen == 0) { printf("String is not initialize!/n"); exit(1); } if ( (1 > pos > s->curlen) || (0 > len > s->curlen - pos)) { printf("Delete position error!/n"); exit(1); } for (i = pos + len; i <= s->curlen; i++) { s->str[i-len] = s->str[i]; } s->curlen -= len; return s; } // 串替換 SeqString* StrReplace(SeqString *s, int pos, int len, SeqString *t) { // 先將t串插入到s串pos+len位置 StrInsert(s, pos + len, t); // 刪除s串pos位置到pos+len位置的元素 StrDel(s, pos, len); return s; } #endif
SeqString.c
/* Name: SeqString.c Version: 0.10 Author: dir Date: 17-06-09 23:31 Description: */ #include "SeqString.h" int main() { char ch[] = "this is "; char tt[] = "a test!"; SeqString s, t, temp; InitStr(&s); InitStr(&t); InitStr(&temp); Strassign(&s, ch); Strassign(&t, tt); printf("SeqString s is:%s/n", s.str); printf("SeqString t is:%s/n", t.str); StrCpy(&temp, &s); printf("StrCpy:%s/n", temp.str); StrCat(&s, &t); printf("StrCat:%s/n", s.str); StrSub(&s, 1, 4, &temp); printf("StrSub:%s/n", temp.str); StrInsert(&s, 8, &t); printf("StrInsert:%s/n", s.str); StrDel(&s, 8, 7); printf("StrDel:%s/n", s.str); StrReplace(&s, 5, 4, &t); printf("StrReplace:%s/n", s.str); system("pause"); return 0; }
運行截圖: