1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include "stdio.h" #include "string.h" #include "stdbool.h"
#define MAXLEN 255
typedef struct { char ch[MAXLEN]; int len; } SString;
void strAssign(SString *S, char chars[]) { for (int i = 1; i < strlen(chars)+1; i++) { S->ch[i] = chars[i]; } S->len = strlen(chars); return; }
void strCopy(SString *S, SString T) { for (int i = 1; i < strlen(T.ch)+1; ++i) { S->ch[i] = T.ch[i]; } S->len = T.len; return; }
bool strEmpty(SString S) { return S.len == 0; }
int strCompare(SString S, SString T) { for (int i = 1; i < S.len+1 && i < T.len+1; ++i) { if (S.ch[i] < T.ch[i]) { return -1; } else if (S.ch[i] > T.ch[i]) { return 1; } } if (S.len > T.len) { return 1; } else if (S.len < T.len) { return -1; } else { return 0; } }
int strLength(SString S) { return S.len; }
SString *subString(SString *Sub, SString S, int pos, int len) { if(pos<1||pos>S.len||len<0||len+pos-1>S.len){ return NULL; } Sub->len=len; for (int i = pos; i < pos+len; ++i) { Sub->ch[i-pos]=S.ch[i]; } return Sub; }
SString *concat(SString *T,SString S1,SString S2) { T->len=S1.len+S2.len; for (int i = 1; i < S1.len+1; ++i) { T->ch[i] = S1.ch[i]; } for (int i = S1.len; i < S1.len+S2.len; ++i) { T->ch[i] = S2.ch[i-S1.len]; } return T; }
int index(SString Sub, SString S) { if (Sub.len > S.len) { return 0; } for (int i = 0; i < S.len - Sub.len; ++i) { int j = 0; while (j < Sub.len && Sub.ch[j + 1] == S.ch[i + j]) { j++; } if (j == Sub.len) { return i; } } return 0; }
void clear(SString *S) { S->len = 0; return; }
void destroy(SString *S) { S->len = 0; S = NULL; return; }
|