vimix

string.cpp

Mar 3rd, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. //Venix Cador
  2. //String ADT
  3. //Cs 23001
  4. #include "string.h"
  5.  
  6. String::String(const int cap){
  7.  
  8. size = cap;
  9. s= new char[size];
  10. s[0] = 0;
  11. length = 0;
  12. }
  13.  
  14. String::String(const char c, const int cap){
  15.  
  16. size = cap;
  17. s = new char[size];
  18. s[0] = c;
  19. s[1] = 0;
  20. length = 1;//because the length is one
  21. }
  22. //copy constructor
  23.  
  24. String::String(const String& rhs, const int cap)
  25. {
  26. //check if capacity is shorter than length
  27. if(cap <= rhs.length){
  28. size = rhs.length + 1;
  29. }else{
  30. size = cap;
  31. }
  32. s = new char[size];
  33.  
  34.  
  35. //copy chars to string
  36. for(length = 0; rhs.s[length] != 0; ++length){
  37. s[length] = rhs.s[length];
  38. }
  39. s[length] = 0;
  40. }
  41. String::String(const char str[], const int cap){
  42.  
  43. //calculate the length of the string
  44. for (length = 0; str[length] != 0; ++length){}
  45.  
  46. //resize the size is needed
  47. if(length > cap - 1) {
  48. size = length + 1;
  49. }else {
  50. size = cap;
  51. }
  52. s = new char[size];
  53.  
  54. //copy chars to string
  55. for(int index = 0; str[index] != 0; ++index){
  56. s[index] = str[index];
  57. }
  58.  
  59. s[length] = 0;
  60. }
  61.  
  62.  
  63. //destructor for string
  64. String::~String()
  65. {
  66. length = 0;
  67. delete [] s;
  68. }
  69.  
  70. //finds the first occurance of a char in a string with zero offset
  71. //Ex: str.finchar('d');
  72. int String::findchar(char find) const
  73. {
  74. for(int i = 0; i < length; ++i){
  75. if(s[i] == find){
  76. return i;
  77. }
  78. }
  79. return -1;
  80. }
  81.  
  82. //finds how many times a string occurs in another string
  83. //Ex: str.findstr(find);
  84.  
  85. int String::findstr(const String& find) const
  86. {
  87. int result = 0;
  88. if( length > find.length){
  89. for(int index = 0, findindex = 0; index < length; ++index, findindex = 0){
  90. while ( s[index + findindex] == find.s[findindex] && findindex <= find.length){
  91. ++findindex;
  92. if (findindex == find.length) {
  93. ++result;
  94. index += findindex;
  95. }
  96. }
  97. }
  98. }
  99. return result;
  100. }
  101. bool String::operator==(const String& rhs) const {
  102.  
  103. if(length != rhs.length) return false;
  104.  
  105. for(int i = 0; i < length; ++i){
  106. if(s[i] != rhs.s[i]) return false;
  107.  
  108. }
  109. return true;
  110. }
  111. /*
  112. *Returns the character from a specified index. Returns null if it is out of bounds.
  113. *Ex: char d = str[1];
  114. */
  115.  
  116. char String::operator [] (int index) const{
  117.  
  118. if( index >= length || index < 0) {
  119. std::exit(0);
  120. }
  121. return s[index];
  122. }
  123. char& String::operator [] (int index){
  124.  
  125. if(index >= length || index < 0){
  126. std:: exit(0);
  127. }
  128. return s[index];
  129. }
  130.  
  131. String String::operator + (const String& rhs) const {
  132.  
  133. int index = 0;
  134.  
  135. String result;
  136.  
  137. result.length = length + rhs.length;
  138.  
  139. // Taking the index and goes to the string in order to find the length of the first string
  140. for(; index <length; ++index)
  141. {
  142. result.s[index] = s[index];
  143. }
  144.  
  145. for(int rhsindex = 0; rhsindex < rhs.length; ++rhsindex, ++index)
  146. {
  147. result.s[index] = rhs.s[rhsindex];
  148. }
  149. result.s[index] = 0;
  150.  
  151. return result;
  152. }
  153. //greater than operator this is fliped compare to the less than operator
  154. bool String::operator < (const String& rhs) const
  155. {
  156. int i = 0;
  157. while((s[i] != 0) && (rhs.s[i] != 0) && (i< MaxString)) {
  158.  
  159. if (s[i] < rhs.s[i]) return true;
  160. if(s[i] == rhs.s[i]) ++i;
  161. else return false;
  162. }
  163.  
  164. if(rhs.s[i] != 0)
  165. return false;
  166.  
  167. return true;
  168.  
  169. }
  170.  
  171. // the output function
  172. std::ostream& operator <<(std::ostream& out, const String& rhs){
  173. int i = 0;
  174. while(rhs[i] != 0){
  175. out<< rhs[i];
  176. ++i;
  177. }
  178. return out;
  179. }
  180. //The input function
  181. std::istream& operator >>(std::istream& in, String& rhs){
  182. char tmp;
  183.  
  184. while (in) {
  185. in.get(tmp);
  186.  
  187. if (!in.eof()) {
  188. rhs += tmp;
  189. }
  190. }
  191.  
  192. return in;
  193. }
  194. //the substr function
  195.  
  196. String String::substr(int left, int right) const
  197. {
  198.  
  199. String result;
  200.  
  201. if (right <= 0 || right > length){
  202. right = length;
  203.  
  204. }
  205.  
  206. for(int i = left; i < right; ++i){
  207. result = result + s[i];
  208. }
  209.  
  210. result.length = right - left;
  211. result[right] = 0;
  212.  
  213. return result;
  214. }
  215. //Swaps two strings
  216. //Ex: str1.swap(str2);
  217.  
  218. //reallocate string's capacity to a specified value
  219. //Ex: str.reallpcate(50);
  220.  
  221. void String::reallocate(const int cap)
  222. {
  223. String temp(*this, cap);
  224. swap(temp);
  225. }
  226. //swap two strings
  227. //Ex: str1.swap(str2)
  228. void String::swap(String& str)
  229. {
  230.  
  231. char *temp = s;
  232. s = str.s;
  233. str.s = temp;
  234.  
  235. int temp_cap = size;
  236. size = str.size;
  237. str.size = temp_cap;
  238.  
  239. int temp_length = length;
  240. length = str.length;
  241. str.length = temp_length;
  242. }
  243. //assignment operator for string
  244. //Ex: String str = rhs_str;
  245. String& String::operator = (String rhs)
  246. {
  247.  
  248. swap(rhs);
  249. return *this;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment