string_lib 1.1 KB

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