C++ String in C


SUBMITTED BY: Guest

DATE: May 2, 2013, 3:43 p.m.

FORMAT: Text only

SIZE: 1.0 kB

HITS: 1151

  1. :3
  2. #ifndef STRING_XLEEK_H
  3. #define STRING_XLEEK_H
  4. #include <stdlib.h>
  5. #include <string.h>
  6. typedef struct string
  7. {
  8. char* str;
  9. int lenght;
  10. } string;
  11. void string_init(string* s)
  12. {
  13. s->lenght = 0;
  14. s->str = malloc(1 * sizeof(char));
  15. s->str[0] = '\0';
  16. }
  17. void string_free(string* s)
  18. {
  19. free(s->str);
  20. s->lenght = 0;
  21. }
  22. void string_set(string* s, const char* c)
  23. {
  24. if(s->lenght > 0)
  25. free(s->str);
  26. s->lenght = strlen(c);
  27. s->str = malloc(s->lenght * sizeof(char) + 1);
  28. strcpy(s->str, c);
  29. }
  30. void string_apps(string* s, string* o)
  31. {
  32. int last = s->lenght;
  33. s->lenght += o->lenght;
  34. s->str = realloc(s->str, s->lenght * sizeof(char) + 1);
  35. strcpy(s->str + last, o->str);
  36. }
  37. void string_appc(string* s, const char* o)
  38. {
  39. string so;
  40. string_set(&so, o);
  41. string_apps(s, &so);
  42. }
  43. #endif

comments powered by Disqus