Problem statement:
We have to take two strings. Then we have to compare them- if they are same we will print “They are same”; else we have to concatenate the second string with the first one.
Class Diagram:
Class string
| Private: Char a[25],b[25]; |
| Public: Input() compare() |
Solution:
| #include<iostream.h> #include<stdio.h> #include<conio.h> #include<string.h> class string{ //declare a class private: char a[25],b[25]; public: void input(){ //input string cout<<"enter string:"<<'\n'; gets(a); cout<<"enter string:"<<'\n'; gets(b); } void compare(){ //compare string if(strcmp(a,b)==0) cout<<"both are same"; else{ strcat(a,b); puts(a);} } }; void main(){ clrscr(); string c; c.input(); c.compare(); getch(); } |
I was unable to solve the program using operator overloading, so I used a simple member function compare(). In this way I solved this problem.
No comments:
Post a Comment