diff options
Diffstat (limited to 'c/dataStructure/string/string.c')
-rwxr-xr-x | c/dataStructure/string/string.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/c/dataStructure/string/string.c b/c/dataStructure/string/string.c new file mode 100755 index 0000000..5b48917 --- /dev/null +++ b/c/dataStructure/string/string.c @@ -0,0 +1,66 @@ +#include "string.h" + +void StrAssign(sstring * st, const char * ch) +{ + size_t size = strlen(ch); + + st->s = (char *) malloc (sizeof(char) * (size+1)); + + if (!st->s) + { + fprintf(stderr,"Failed to malloc!\n"); + exit(EXIT_FAILURE); + } + + strncpy(st->s,ch,size+1); + st->length = size; +} + +void StrCopy(sstring * st, const sstring * sp) +{ + if (!sp->s) + { + fprintf(stderr,"Empty string!\n"); + exit(EXIT_FAILURE); + } + + StrAssign(st,sp->s); +} + +bool StrEmpty(sstring * st) +{ + if (!st->s) + return false; + return true; +} + +int StrCompare(const sstring * st, const sstring * sp) +{ + int count = 0, i; + if (!st->s || !sp->s) + { + fprintf(stderr,"Empty string!\n"); + exit(EXIT_FAILURE); + } + + for (i = 0; st->s[i] && sp->s[i]; i++) + { + if (st->s[i] > sp->s[i]) + count++; + else if (st->s[i] < sp->s[i]) + count--; + } + + if (st->s[i]) + { + i++; + count++; + } + else if (sp->[i]) + { + i++; + count--; + } + + return count; +}
\ No newline at end of file |