#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;
}