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