string_lib 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #define cstring const int* #end
  2. #define string int* #end
  3. string concat(cstring a, cstring b) {
  4. int la = length(a);
  5. int lb = length(b);
  6. int* s = new int[la + lb];
  7. for(int i = 0; i < la; i++) {
  8. s[i] = a[i];
  9. }
  10. for(int i = 0; i < lb; i++) {
  11. s[i + la] = b[i];
  12. }
  13. return s;
  14. }
  15. bool compare(cstring a, cstring b) {
  16. int l = length(a);
  17. if(l != length(b)) {
  18. return false;
  19. }
  20. for(int i = 0; i < l; i++) {
  21. if(a[i] != b[i]) {
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
  27. void toLower(string s) {
  28. int l = length(s);
  29. for(int i = 0; i < l; i++) {
  30. int c = s[i];
  31. if(c >= 'A' && c <= 'Z') {
  32. c = c - 'A' + 'a';
  33. s[i] = c;
  34. }
  35. }
  36. }
  37. void main() {
  38. cstring msg = "Hallo User: ";
  39. cstring name = "Kajetan";
  40. string together = concat(msg, name);
  41. string together2 = concat(together, name);
  42. test(together2);
  43. toLower(together);
  44. test(together);
  45. delete together;
  46. delete together2;
  47. cstring a = name;
  48. test(a);
  49. test(a == name);
  50. cstring baum = "Kajetan";
  51. test(name == baum);
  52. test(compare(name, baum));
  53. test(compare(name, "Kajetan"));
  54. test(compare(name, "Kajetaa"));
  55. }