-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.h
148 lines (129 loc) · 2.93 KB
/
String.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef STRING_H_
#define STRING_H_
#include<iostream>
#include<cstring>
using namespace std;
namespace STRING {
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIMIT = 80;
public:
//constructors and methods
String(const char *s);//constructor
String();
String(const String &);
~String();
int length()const { return len; };
char *strval()const { return str; };
//overloaded operator methods
String &operator = (const String &);
String &operator = (const char*);
char &operator[](int i);
const char & operator[](int i)const;
//overloaded operator friends
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st, const String & st2);
friend ostream& operator<<(ostream & os, const String &st);
friend istream& operator>>(istream &is, String &st);
//static function
static int howmany();
};
//initialize static class member
int String::num_strings = 0;
//static method
int String::howmany()
{
return num_strings;
};
//class methods
String::String(const char *s)
{
len = std::strlen(s);
str = new char[len + 1];
strcpy(str, s);
num_strings++;
};
String::String()
{
len = 4;
str = new char[1];
str[0] = '\0';
num_strings++;
};
String::String(const String &st)
{
num_strings++;
len = st.length();
str = new char[len + 1];//allocate space
strcpy(str, st.strval());
};
String::~String()
{
--num_strings;
delete[] str;
};
//overloaded operator methods
String &String::operator=(const String &st)
{
if (this == &st)
return *this;
delete[] str;
len = st.length();
str = new char[len + 1];
strcpy(str,st.strval());
return *this;
};
String &String::operator=(const char *s)
{
delete[] str;
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
return *this;
};
//read-write char access for non-constant String
char &String::operator[](int i)
{
return str[i];
};
//read-only char access for const String
const char& String::operator[](int i)const
{
return str[i];
};
//overloaded operator friends
bool operator<(const String &st1, const String &st2)
{
return (std::strcmp(st1.strval(), st2.strval()));
};
bool operator>(const String&st1, const String &st2)
{
return st2.strval() < st1.strval();
};
bool operator==(const String & st1, const String &st2)
{
return (strcmp(st1.strval(), st2.strval()) == 0);
};
ostream &operator<<(ostream &os, const String &st)
{
os << st.strval();
return os;
};
//quick and dirty input
istream &operator>>(istream &is, String &st)
{
char temp[String::CINLIMIT];
is.get(temp, String::CINLIMIT);
if (is)
st = temp;
while (is && is.get() != '\n')
continue;
return is;
};
}
#endif