Advertisement
Guest User

string.cpp

a guest
Mar 13th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. const int DEFAULT_STRING_CAPACITY = 256;
  4.  
  5. class String {
  6. public:
  7. String ();
  8. String (const char);
  9. String (const char*);
  10. String (const int, const char*);
  11. String (int);
  12. String (const String&); // Copy constructor
  13. ~String (); // Destructor
  14.  
  15. String& operator= (String); // Assignment operator
  16. void resetCapacity (int);
  17. void swap (String&);
  18.  
  19. char& operator[] (int);
  20. char operator[] (int) const;
  21. int capacity () const;
  22. int length () const;
  23. bool operator== (const String&) const;
  24. bool operator< (const String&) const;
  25. String operator+ (const String&) const;
  26.  
  27. String substr (int, int) const;
  28. int findstr (const String&) const;
  29. int findchar (const char) const;
  30. int nextBlank (const int) const;
  31. int nextNonBlank (const int) const;
  32. String justify (const int) const;
  33.  
  34. friend std::ostream& operator<<(std::ostream&, const String&);
  35. friend std::istream& operator>>(std::istream&, String&);
  36.  
  37. private:
  38. char *S;
  39. int cap;
  40. };
  41.  
  42. String operator+ (const char[], const String&);
  43. String operator+ (const char, const String&);
  44. bool operator== (const char[], const String&);
  45. bool operator== (const char, const String&);
  46. bool operator< (const char[], const String&);
  47. bool operator< (const char, const String&);
  48. bool operator<= (const String&, const String&);
  49. bool operator!= (const String&, const String&);
  50. bool operator>= (const String&, const String&);
  51. bool operator> (const String&, const String&);
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. #include "string.hpp"
  71. #include<cassert>
  72.  
  73.  
  74. String::String()
  75. {
  76. cap = DEFAULT_STRING_CAPACITY;
  77. S = new char[0];
  78. }
  79.  
  80.  
  81. String::String(const char Ch)
  82. {
  83. cap = DEFAULT_STRING_CAPACITY;
  84. S = new char[cap];
  85. S[0] = Ch;
  86. S[1] = '\0'; //NULL Term. char
  87. }
  88.  
  89.  
  90. String::String(const char *Temp)
  91. {
  92. cap = DEFAULT_STRING_CAPACITY;
  93. S = new char[cap];
  94.  
  95. int i = 0;
  96.  
  97. while(Temp[i] != '\0')
  98. {
  99. S[i] = Temp[i];
  100. ++i;
  101. }
  102.  
  103. S[i] = '\0'; //NULL terminating
  104.  
  105. }
  106.  
  107.  
  108.  
  109. String::String(int Num)
  110. {
  111. cap = Num;
  112. S = new char[cap];
  113. }
  114.  
  115.  
  116. String::String(const int Num, const char *Temp)
  117. {
  118. cap = Num;
  119. S = new char[cap];
  120.  
  121. int i = 0;
  122.  
  123. while(Temp[i] != 0 && i < cap)
  124. {
  125. S[i] = Temp[i];
  126. ++i;
  127. }
  128.  
  129. S[i] = '\0';
  130. }
  131.  
  132.  
  133. String::String(const String& Actual)
  134. {
  135. cap = Actual.cap;
  136. S = new char[cap];
  137.  
  138. for(int i = 0; i < cap; ++i)
  139. S[i] = Actual.S[i];
  140. }
  141.  
  142.  
  143. String::~String()
  144. {
  145. delete[] S;
  146. }
  147.  
  148.  
  149. String& String::operator=(String Rhs)
  150. {
  151. if(S == Rhs.S)
  152. return *this;
  153.  
  154. delete[] S;
  155.  
  156. cap = Rhs.cap;
  157. S = new char[cap];
  158.  
  159. for(int i = 0; i < cap; ++i)
  160. S[i] = Rhs.S[i];
  161. //swap(Rhs);
  162.  
  163. return *this;
  164. }
  165.  
  166.  
  167. char& String::operator[](int Num)
  168. {
  169. if(Num < 0 || Num >= length())
  170. return S[Num] = 0;
  171.  
  172. return S[Num];
  173. }
  174.  
  175.  
  176. char String::operator[](int Num) const
  177. {
  178. if(Num >= 0 && Num < length())
  179. return S[Num];
  180. else
  181. return 0;
  182. }
  183.  
  184.  
  185. int String::capacity() const
  186. {
  187. return cap;
  188. }
  189.  
  190.  
  191. int String::length() const
  192. {
  193. int result = 0;
  194.  
  195. while(S[result] != '\0')
  196. ++result;
  197.  
  198. return result;
  199. }
  200.  
  201.  
  202. bool String::operator==(const String& Rhs) const
  203. {
  204. int i = 0;
  205.  
  206. while( (S[i] != 0) && (Rhs[i] != 0) && (S[i] == Rhs[i]) )
  207. ++i;
  208.  
  209. if(S[i] == Rhs[i])
  210. return true;
  211.  
  212. return false;
  213. }
  214.  
  215.  
  216. bool String::operator<(const String& Rhs) const
  217. {
  218. int i = 0;
  219.  
  220. while( (S[i] != 0) && (Rhs.S[i] != 0) && (S[i] == Rhs[i]) )
  221. ++i;
  222.  
  223. if( (S[i] == 0) && (Rhs[i] == 0) )
  224. return false;
  225.  
  226. if(S[i] < Rhs.S[i])
  227. return true;
  228.  
  229. if(S[i] == 0)
  230. return true;
  231.  
  232. return false;
  233. }
  234.  
  235.  
  236. String String::operator+(const String& Rhs) const
  237. {
  238. String result(S);
  239. int offset = length();
  240. int i = 0;
  241.  
  242. while(Rhs.S[i] != 0)
  243. {
  244. result.S[offset + i] = Rhs.S[i];
  245. ++i;
  246. }
  247.  
  248. result.S[offset + i] = '\0';
  249. return result;
  250. }
  251.  
  252.  
  253. std::ostream& operator<<(std::ostream& Out, const String& Rhs)
  254. {
  255. Out << Rhs.S;
  256. return Out;
  257. }
  258.  
  259.  
  260. std::istream& operator>>(std::istream& In, String& Rhs)
  261. {
  262. char buf[DEFAULT_STRING_CAPACITY];
  263. In >> buf;
  264. Rhs = String(buf);
  265. return In;
  266. }
  267.  
  268.  
  269. String operator+(const char* Lhs, const String& Rhs)
  270. {
  271. String result = Lhs;
  272. result = result + Rhs;
  273. return result;
  274. }
  275.  
  276.  
  277. String operator+(const char Lhs, const String& Rhs)
  278. {
  279. String result = Lhs;
  280. result = result + Rhs;
  281. return result;
  282. }
  283.  
  284.  
  285. bool operator==(const char* Lhs, const String& Rhs)
  286. {
  287. String result = Lhs;
  288.  
  289. if(result == Rhs)
  290. return true;
  291.  
  292. return false;
  293. }
  294.  
  295.  
  296. bool operator==(const char Lhs, const String& Rhs)
  297. {
  298. String result = Lhs;
  299.  
  300. if(result == Rhs)
  301. return true;
  302.  
  303. return false;
  304. }
  305.  
  306.  
  307. bool operator<(const char* Lhs, const String& Rhs)
  308. {
  309. String result = Lhs;
  310.  
  311. if(result < Rhs)
  312. return true;
  313.  
  314. return false;
  315. }
  316.  
  317.  
  318. bool operator<(const char Lhs, const String& Rhs)
  319. {
  320. String result = Lhs;
  321.  
  322. if(result < Rhs)
  323. return true;
  324.  
  325. return false;
  326. }
  327.  
  328.  
  329. bool operator<=(const String& Lhs, const String& Rhs)
  330. {
  331. if(Lhs < Rhs || Lhs == Rhs)
  332. return true;
  333.  
  334. return false;
  335. }
  336.  
  337.  
  338. bool operator!=(const String& Lhs, const String& Rhs)
  339. {
  340. if(!(Lhs.operator==(Rhs)) )
  341. return true;
  342.  
  343. return false;
  344. }
  345.  
  346.  
  347. bool operator>=(const String& Lhs, const String& Rhs)
  348. {
  349. if(!(Lhs.operator<(Rhs)) || (Lhs == Rhs) )
  350. return true;
  351.  
  352. return false;
  353. }
  354.  
  355.  
  356.  
  357. bool operator>(const String& Lhs, const String& Rhs)
  358. {
  359. if(!(Lhs.operator<(Rhs)) && (Lhs != Rhs) )
  360. return true;
  361.  
  362. return false;
  363. }
  364.  
  365.  
  366. String String::substr(int Start, int End) const
  367. {
  368. String result;
  369. assert(End <= length());
  370.  
  371. for(int i = Start; i <= End; ++i)
  372. result = result + S[i];
  373.  
  374. return result;
  375. }
  376.  
  377.  
  378. // Finds fisrt match in string
  379. // Returns -1 if no match
  380. int String::findstr(const String& Search) const
  381. {
  382. int count = 0;
  383. int start = 0;
  384. int i = 0;
  385.  
  386. while(i <= length())
  387. {
  388. if(S[i] == Search.S[count])
  389. {
  390. ++count;
  391. start = i;
  392. }
  393.  
  394. ++i;
  395.  
  396. if(count == Search.length())
  397. return start - (count - 1);
  398. }
  399.  
  400. if(count != Search.length())
  401. return -1;
  402.  
  403. return -1;
  404. }
  405.  
  406.  
  407.  
  408. int String::findchar(const char Search) const
  409. {
  410. int i = 0;
  411.  
  412. while(i <= length())
  413. {
  414. if(S[i] == Search)
  415. {
  416. return i;
  417. }
  418.  
  419. else if(i == length())
  420. return -1;
  421.  
  422. ++i;
  423. }
  424.  
  425. return -1;
  426. }
  427.  
  428.  
  429. void String::resetCapacity(int Newcap)
  430. {
  431. char *temp = new char[Newcap];
  432. int smallest = cap;
  433.  
  434. if(cap > Newcap)
  435. smallest = Newcap;
  436.  
  437. for(int i = 0; i < smallest; ++i)
  438. temp[i] = S[i];
  439.  
  440. delete[] S;
  441. S = temp;
  442. cap = Newcap;
  443. }
  444.  
  445.  
  446. void String::swap(String& Rhs)
  447. {
  448. char *strtmp = S;
  449. S = Rhs.S;
  450. Rhs.S = strtmp;
  451.  
  452. int captmp = cap;
  453. cap = Rhs.cap;
  454. Rhs.cap = captmp;
  455. }
  456.  
  457.  
  458. int String::nextBlank(const int Index) const
  459. {
  460. int i = Index;
  461.  
  462. while(i <= length())
  463. {
  464. if(S[i] == ' ' && (i != Index || S[0] == ' '))
  465. {
  466. return i;
  467. }
  468.  
  469. else if(i == length())
  470. return -1;
  471.  
  472. ++i;
  473. }
  474.  
  475. return -1;
  476. }
  477.  
  478.  
  479. int String::nextNonBlank(const int Index) const
  480. {
  481. int i = Index;
  482.  
  483. while(i <= length())
  484. {
  485. if(S[i] != ' ' && i != Index )
  486. {
  487. return i;
  488. }
  489.  
  490. ++i;
  491. }
  492.  
  493. return -1;
  494. }
  495.  
  496.  
  497. String String::justify(const int Width) const
  498. {
  499. String result = *this;
  500. int i = 0;
  501.  
  502. if(nextBlank(0) == -1 || length() > Width)
  503. return *this;
  504.  
  505. while(result.length() < Width)
  506. {
  507. i = result.nextBlank(i);
  508. result = result.substr(0,i) + " " +result.substr(i + 1, result.length());
  509. i = result.nextNonBlank(i);
  510. }
  511.  
  512. return result;
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement