-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator overloading.cpp
More file actions
160 lines (158 loc) · 4 KB
/
operator overloading.cpp
File metadata and controls
160 lines (158 loc) · 4 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
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
149
150
151
152
153
154
155
156
157
158
159
160
/*Exp4 Implement a class string containing the following functions:
a. Overload + operator to carry out the concatenation of strings
b. Overload = operator to carry out string copy.
c. Overload <= operator to carry out the comparison of strings.
d. Function to display the length of a string.
e. Function tolower() to convert upper case letters to lower case.
f. Function toupper() to convert lower case letters to upper case.*/
#include<iostream>
#include<string.h>
using namespace std;
class String
{
char *s;
int size;
public:
String(char *c)
{
size = strlen(c);
s = new char[size];
strcpy(s,c);
}
char* operator +(char *s1)
{
size = strlen(s)+strlen(s1);
char *s2 = new char[strlen(s)];
strcpy(s2,s);
s = new char[size];
strcpy(s,s2);
strcat(s,s1);
return s;
}
char* operator =(char *s1)
{
size = strlen(s1);
s = new char[size];
strcpy(s,s1);
return s;
}
bool operator <=(char *s1)
{
return strcmp(s,s1);
}
void display()
{
cout<<"\n\n String stored in class = "<<s;
}
void displaylength()
{
cout<<"\n\n Length of string stored in class = "<<size;
}
void Tolower()
{
cout<<"\n\n String converted to lowercase";
for(int i=0;i<size;i++)
{
if(isupper(s[i]))
s[i] = (char)tolower(s[i]);
}
display();
}
void Toupper()
{
cout<<"\n\n String converted to uppercase";
for(int i=0;i<size;i++)
{
if(islower(s[i]))
s[i] = (char)toupper(s[i]);
}
display();
}
};
int main()
{
char *s1;
int choice,l1;
cout<<"\n\n Program to perform operations on string";
cout<<"\n\n Enter length of string - ";
cin>>l1;
fflush(stdin);
s1 = new char[l1];
cout<<"\n Enter string to be stored in class - ";
gets(s1);
String s(s1);
while(1)
{
char *d;
int length;
cout<<"\n\n Menu\n 1. String concatenation \n 2. String copy \n 3. String comparison \n 4. Display String \n 5. Display length of string \n 6. Convert string to lowercase \n 7. Convert string to uppercase \n 8. Exit ";
cin>>choice;
if(choice==1)
{
s=s1;
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
s = s+d;
cout<<"\n After concatenation....";
s.display();
}
else if(choice==2)
{
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
s=d;
cout<<"\n After copying....";
s.display();
}
else if(choice==3)
{
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
if(!(s<=d))
{
cout<<"\n Strings are equal";
}
else
{
cout<<"\n Strings are not equal";
}
}
else if(choice==4)
{
s.display();
}
else if(choice==5)
{
s.displaylength();
}
else if(choice==6)
{
s.Tolower();
}
else if(choice==7)
{
s.Toupper();
}
else if(choice==8)
{
exit(0);
}
else
{
cout<<"\n\n Wrong choice";
}
}
return 0;
}