-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.cpp
82 lines (68 loc) · 1.19 KB
/
string.cpp
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
/* C++ program to manipulate STRING
* Written By: Ramchandra Shahi Thakuri
* Date:
*/
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
class String
{
private:
char *str;
int length;
public:
String()
{
length = 0;
str = new(nothrow) char[length + 1];
if(!str)
{
cout << "\nInsuff. memory\n";
exit(0);
}
}
String(char *s)
{
length = strlen(s);
str = new(nothrow) char[length + 1];
if(!str)
{
cout << "\nInsuff. memory\n";
exit(0);
}
strcpy(str, s);
}
friend String operator +(String, String);
friend ostream &operator <<(ostream &, String);
};
String operator +(String s1, String s2)
{
String s3;
s3.length = strlen(s1.str) + strlen(s2.str);
s3.str = new(nothrow) char[s3.length + 1];
if(!s3.str)
{
cout << "\nInsuff. memory\n";
exit(0);
}
strcpy(s3.str, s1.str);
strcat(s3.str, " ");
strcat(s3.str, s2.str);
return s3;
}
ostream &operator <<(ostream &out, String s)
{
out << s.str << endl;
return out;
}
int main()
{
String s1("Dr. Ambedkar"), s2("Institute of Technology"), s3;
s3 = s1 + s2;
//now displaying STRING
cout << s1;
cout << s2;
cout << s3;
return 0;
}