Advertisement
Solingen

string.cpp

Nov 1st, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include "string.h"
  4.  
  5. class String;
  6.  
  7. String::String()
  8. {
  9.     len = 0;
  10.     str = new char[1];
  11.     str[0] = '\0';
  12. }
  13. String::String(const char* s)
  14. {
  15.     len = strlen(s);
  16.     str = new char[len + 1];
  17.     strcpy(str, s);
  18. }
  19. String::String(const String& other)
  20. {
  21.     len = other.len;
  22.     str = new char[len + 1];
  23.     strcpy(str, other.str);
  24. }
  25.  
  26. String& String::operator=(const String& other)
  27. {
  28.     if (this == &other)
  29.         return *this;
  30.  
  31.     delete[] str;
  32.     len = other.len;
  33.     str = new char[len + 1];
  34.     strcpy(str, other.str);
  35.     return *this;
  36. }
  37.  
  38. WordIterator String::split(const String& separators)
  39. {
  40.     WordIterator result;
  41.     char* p = this->str;
  42.     while(p)
  43.     {
  44.         p = skip_spaces(p, separators);
  45.         if (!*p)
  46.             break;
  47.        
  48.         char* word = p;
  49.         p = skip_until_spaces(p, separators);
  50.         int  len = p-word;
  51.         char* wordcc = new char[len+1];
  52.         strncpy(wordcc, word, len);
  53.         wordcc[len] = 0;
  54.         result.add(wordcc);
  55.         delete[] wordcc;
  56.     }
  57.     return result;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement