Skip to content

Commit 5fcf12f

Browse files
create project dir
1 parent ae2c399 commit 5fcf12f

5 files changed

Lines changed: 696 additions & 0 deletions

File tree

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiler and flags
2+
CXX = g++
3+
CXXFLAGS = -std=c++11 -Wall
4+
5+
# Source files
6+
SRC = main.cpp String.cpp StringFriend.cpp
7+
8+
# Output executable
9+
OUT = app
10+
11+
# Default target to build the project
12+
all: $(OUT)
13+
14+
# Rule to build the executable
15+
$(OUT): $(SRC)
16+
$(CXX) $(CXXFLAGS) $(SRC) -o $(OUT)
17+
rm -f $(SRC:.cpp=.o) # Remove object files after linking
18+
19+
# Clean up generated files
20+
clean:
21+
rm -f $(OUT)
22+
23+

include/String.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// String.h
2+
#ifndef STRING_H
3+
#define STRING_H
4+
5+
#include<iostream>
6+
#include<cstdio>
7+
#include<fstream>
8+
using namespace std;
9+
10+
// Non member function prototype
11+
unsigned int mystrlen(const char *ptr);
12+
void mystrcpy(char*, const char*, int);
13+
14+
/*** String class implementation ***/
15+
class String{
16+
private:
17+
char *str;
18+
unsigned int length;
19+
20+
public:
21+
/*** Constructor ***/
22+
String(); // default
23+
String(const char* p); // parameterised
24+
String(String&& other); // move
25+
String(String& t); // deep copy
26+
~String();
27+
/*** member function ***/
28+
void getstr(void);
29+
int len(void);
30+
char* begin(void);
31+
char* end(void);
32+
33+
/*** operator overloaded member function ***/
34+
String& operator=(String& s2);
35+
String& operator=(const char* ptr);
36+
String& operator=(String&& other); // Move assignment
37+
String operator +(String&);
38+
String& operator +=(String&);
39+
char& operator [](int i);
40+
bool operator >(String s2);
41+
bool operator <(String s2);
42+
bool operator >=(String s2);
43+
bool operator <=(String s2);
44+
bool operator !=(String s2);
45+
bool operator ==(String s2);
46+
47+
friend ostream& operator <<(ostream& out, String& obj);
48+
friend istream& operator >>(istream& in, String& obj);
49+
friend String operator+(String& obj, const char* ptr);
50+
friend String operator+(const char* ptr, String& obj);
51+
friend String operator+(String& obj, const char ch);
52+
friend String operator+(const char ch, String& obj);
53+
54+
/*** friend function ***/
55+
friend unsigned int my_strlen(String& s);
56+
friend void my_strcpy(String& s1, String& s2);
57+
friend void my_strncpy(String& dest, const String& src, unsigned const int len);
58+
friend int my_strcmp(const String& s1, const String& s2);
59+
friend void my_strcat(String& s1, const String& s2);
60+
friend void my_strncat(String& s1, const String& s2, unsigned const int len);
61+
friend String& my_strrev1(String& str);
62+
friend void my_strrev2(String& startAddr, String& endAddr);
63+
friend String& my_strupper(String& str);
64+
friend String& my_strlower(String& str);
65+
friend bool my_strchr(String& str, char ch);
66+
friend bool my_strrchr(String& str, char ch);
67+
friend bool my_strstr(const String& mainstr, const String& substr);
68+
};
69+
70+
#endif // STRING_H
71+

src/String.cpp

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
/*** default constructor; string s1; ***/
2+
String :: String():str(nullptr), length(0){}
3+
4+
/*** parameterised constructor; string s2("Ranjan") ***/
5+
String :: String(const char* p){
6+
length = mystrlen(p);
7+
str = new char[length+1];
8+
mystrcpy(str, p, length);
9+
}
10+
String::String(String&& other) {
11+
// Transfer ownership of resources
12+
str = other.str;
13+
length = other.length;
14+
15+
// Null out the source
16+
other.str = nullptr;
17+
other.length = 0;
18+
}
19+
/*** deep copy constructor; s2 = s1; ***/
20+
String :: String(String& t){
21+
length = t.length;
22+
//length = mystrlen(t.str);
23+
str = new char[length + 1];
24+
mystrcpy(str, t.str, length);
25+
}
26+
27+
/*** member function ***/
28+
void String :: getstr(){
29+
if(str==nullptr) return;
30+
cout<<str<<endl;
31+
}
32+
int String::len(){
33+
return length;
34+
}
35+
char* String :: begin(void){
36+
return str;
37+
}
38+
39+
char* String :: end(void){
40+
//length = strlen(str);
41+
return str+length-1;
42+
}
43+
44+
/*** operator overloaded member function ***/
45+
String& String :: operator =(String& s1){ // s2 = s1;
46+
length = s1.length;
47+
delete str;
48+
str = new char[length+1];
49+
50+
mystrcpy(str, s1.str, length);
51+
return *this;
52+
}
53+
String& String :: operator = (const char* ptr){
54+
length = mystrlen(ptr);
55+
delete str;
56+
str = new char[length+1];
57+
mystrcpy(str, ptr, length);
58+
59+
return *this;
60+
}
61+
String& String::operator=(String&& other) {
62+
if (this != &other) {
63+
delete[] str; // Free existing memory
64+
str = other.str; // Steal the resource
65+
length = other.length;
66+
67+
other.str = nullptr; // Nullify source
68+
other.length = 0;
69+
}
70+
return *this;
71+
}
72+
String String :: operator +(String& s1){ // +ope principal: doesn't modify its operands
73+
String result;
74+
result.length = length + s1.length;
75+
result.str = new char[result.length +1];
76+
mystrcpy(result.str, str, result.length);
77+
78+
for(int i=0; s1.str[i]; i++){
79+
result.str[length + i] = s1.str[i]; // overwrite '\0' with first character of s2.str
80+
}
81+
result.str[result.length] = '\0';
82+
return result; // call move constructor rather than copy
83+
84+
}
85+
String& String :: operator +=(String& s1){
86+
int i, j = length;
87+
length = length + s1.length;
88+
char* temp = new char[length + 1]; // str ko abhi free nhi kr skte
89+
mystrcpy(temp, str, length);
90+
for(i=0; s1.str[i]; i++){
91+
temp[j++] = s1.str[i]; // overwrite '\0' with first character of s1.str
92+
}
93+
temp[j] = '\0';
94+
delete str; // free resource pointed by str as content already copied to temp;
95+
str = temp; // str point the resource pointed by temp;
96+
temp = nullptr; // avoid temp to become dangling pointer
97+
return *this; // safely return the object reference
98+
99+
}
100+
char& String :: operator [](int index){
101+
if(str==nullptr){
102+
str = new char[10];
103+
return str[index];
104+
}
105+
if (index < 0 || index > length)
106+
throw out_of_range("Index out of range");
107+
return str[index];
108+
}
109+
bool String :: operator >(String s2){
110+
int i = 0;
111+
while(*(str+i) && *(s2.str+i)){
112+
if(*(str + i) != *(s2.str + i)){
113+
return *(str+i) > *(s2.str+i);
114+
}
115+
i++;
116+
}
117+
return *(str+i) > *(s2.str+i);
118+
}
119+
bool String :: operator <(String s2){
120+
int i = 0;
121+
while(*(str+i) && *(s2.str+i)){
122+
if(*(str + i) != *(s2.str + i)){
123+
return *(str+i) < *(s2.str+i);
124+
}
125+
i++;
126+
}
127+
return *(str+i) < *(s2.str+i);
128+
129+
}
130+
bool String :: operator >=(String s2){
131+
int i = 0;
132+
while(*(str+i) && *(s2.str+i)){
133+
if(*(str + i) != *(s2.str + i)){
134+
return *(str+i) >= *(s2.str+i);
135+
}
136+
i++;
137+
}
138+
return *(str+i) >= *(s2.str+i);
139+
140+
}
141+
bool String :: operator <=(String s2){
142+
int i = 0;
143+
while(*(str+i) && *(s2.str+i)){
144+
if(*(str + i) != *(s2.str + i)){
145+
return *(str+i) <= *(s2.str+i);
146+
}
147+
i++;
148+
}
149+
return *(str+i) <= *(s2.str+i);
150+
151+
}
152+
bool String :: operator != (String s2){
153+
int i = 0;
154+
while(*(str+i) && *(s2.str+i)){
155+
if(*(str + i) != *(s2.str + i)){
156+
return *(str+i) != *(s2.str+i);
157+
}
158+
i++;
159+
}
160+
return *(str+i) != *(s2.str+i);
161+
162+
}
163+
bool String :: operator ==(String s2){
164+
int i = 0;
165+
while(*(str+i) && *(s2.str+i)){
166+
if(*(str + i) != *(s2.str + i)){
167+
return *(str+i) == *(s2.str+i);
168+
}
169+
i++;
170+
}
171+
return *(str+i) == *(s2.str+i);
172+
173+
/*int i = 0;
174+
while(*(str+i)){
175+
if(*(str+i) != *(s2.str +i)){ // s2.str; might unauthorize memory access after '\0'
176+
return 0;
177+
}
178+
i++;
179+
}
180+
if(*(str+i) == '\0' && *(s2.str + i) == '\0')
181+
return 1;
182+
return 0;*/
183+
}
184+
185+
/*** overloaded operator friend function ***/
186+
ostream& operator <<(ostream& out, String& obj){
187+
out<<obj.str;
188+
return out;
189+
}
190+
istream& operator >>(istream& in, String& obj){
191+
if(obj.str == nullptr){ // String s4; cin>>s4; cout<<s4<<endl;
192+
193+
fstream fin("input.txt", ios::in | ios::out | ios::trunc);
194+
if(!fin.is_open()) {
195+
cerr << "Error opening file\n";
196+
exit(1);
197+
}
198+
199+
char ch;
200+
int count = 0, i =0;
201+
202+
// Read characters from user and store to file
203+
while(1)
204+
{
205+
cin.get(ch);
206+
fin.put(ch);
207+
if (ch == '\n') break;
208+
count++;
209+
}
210+
211+
obj.str = new char[count+1];
212+
obj.length = count;
213+
214+
// Rewind file to beginning
215+
fin.seekg(0, ios::beg);
216+
while(1){
217+
fin.get(ch);
218+
if(ch == '\n'){
219+
obj.str[i] = '\0';
220+
break;
221+
}
222+
obj.str[i++] = ch;
223+
}
224+
fin.close();
225+
remove("input.txt"); // delete file to unnecessary fill hardisk
226+
return in;
227+
}
228+
in>>obj.str;
229+
return in;
230+
}
231+
String operator+(String& obj, const char* ptr){
232+
obj.length = obj.length + mystrlen(ptr);
233+
String temp;
234+
temp.str = new char[obj.length + 1];
235+
temp.length = obj.length;
236+
int i=0, j= 0;
237+
while(obj.str[i]){
238+
temp.str[i] = obj.str[i];
239+
i++;
240+
}
241+
while(ptr[j]){
242+
temp.str[i++] = ptr[j++];
243+
}
244+
temp.str[i]= '\0';
245+
246+
return temp;
247+
}
248+
String operator+(const char* ptr, String& obj){
249+
String temp;
250+
temp.length = obj.length + mystrlen(ptr);
251+
temp.str = new char[obj.length + 1];
252+
253+
int i=0, j= 0;
254+
255+
while(ptr[i]){
256+
temp.str[i] = ptr[i];
257+
i++;
258+
}
259+
while(obj.str[j]){
260+
temp.str[i++] = obj.str[j++];
261+
}
262+
temp.str[i]= '\0';
263+
264+
return temp;
265+
}
266+
String operator+(String& obj, const char ch){
267+
String temp;
268+
temp.length = obj.length + 1;
269+
temp.str = new char[temp.length + 1];
270+
int i = 0;
271+
while(obj.str[i]){
272+
temp.str[i] = obj.str[i];
273+
i++;
274+
}
275+
temp.str[i++] = ch;
276+
temp.str[i] = '\0';
277+
278+
return temp;
279+
280+
}
281+
String operator+(const char ch, String& obj){
282+
String temp;
283+
temp.length = obj.length + 1;
284+
temp.str = new char[temp.length + 1];
285+
int i = 0, j=0;
286+
temp.str[i++] = ch;
287+
while(obj.str[j]){
288+
temp.str[i++] = obj.str[j++];
289+
}
290+
291+
temp.str[i] = '\0';
292+
293+
return temp;
294+
}
295+
296+
~String(){
297+
delete[] str;
298+
cout<<"memory deallocated"<<endl;
299+
}

0 commit comments

Comments
 (0)