-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
116 lines (96 loc) · 1.91 KB
/
String.cpp
File metadata and controls
116 lines (96 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Text.h"
#include <stdlib.h> //needed for atoi and atof
#include <cstring> //needed for strlen and strcmp
#include <sstream>
#include <iostream>
using namespace std;
String::String(const char* char_array)
{
sz = strlen(char_array);
char* text = new char[sz+1];
for (int i = 0; i < sz; i++)
{
text[i] = char_array[i];
}
text[sz] = 0; //null terminator
this->text = text;
}
String::~String()
{
delete[] text;
}
//zero-based
char String::charAt(int index)
{
if (index < 0 || index >= sz) return -1;
return text[index];
}
const char* String::getText()
{
return text;
}
int String::length()
{
return sz;
}
int String::compare(String* other)
{
return strcmp(text, other->text);
}
void String::displayString()
{
cout << text;
}
int String::find(char delimiter, int start)
{
if (start >= sz || start < 0) return -1;
int loc = sz;
for (int i = start; i < sz; i++)
{
if (text[i] == delimiter)
{
loc = i;
break;
}
}
return loc; //delimiter not found
}
//the substring will use the characters from start to end inclusive
String* String::substr(int start, int end)
{
if (start > end || start < 0) return NULL;
if (start > sz || end > sz) return NULL;
int sub_len = end - start + 1;
char* sub_text = new char[sub_len + 1];
int count = 0;
for (int i = start; i <= end; i++)
{
sub_text[count] = text[i];
count++;
}
sub_text[count] = 0;
String* sub = new String((const char*) sub_text);
return sub;
}
int String::a_to_i()
{
return atoi(text);
}
float String::a_to_f()
{
return atof(text);
}
String* String::i_to_a(int number)
{
stringstream out;
out << number;
const char* text = out.str().c_str();
return new String(text);
}
String* String::f_to_a(float number)
{
stringstream out;
out << number;
const char* text = out.str().c_str();
return new String(text);
}