Skip to content

Instantly share code, notes, and snippets.

@zeerorg
Created October 18, 2016 14:36
Show Gist options
  • Save zeerorg/cee949e0379c961803a94593a7e6e6d5 to your computer and use it in GitHub Desktop.
Save zeerorg/cee949e0379c961803a94593a7e6e6d5 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class strtype
{
private:
char* str;
int len;
public:
strtype();
strtype(char*);
strtype(strtype&);
strtype operator+(strtype&);
// strtype operator=(strtype&);
void operator=(char*);
// void operator=(strtype&, strtype&);
bool operator>(strtype&);
bool operator<(strtype&);
bool operator==(strtype&);
void display();
void get();
};
strtype::strtype(strtype& s)
{
str = new char[s.len+1];
strcpy(str, s.str);
len = s.len;
}
strtype::strtype()
{
str = new char[1];
strcpy(str, "");
len = 0;
}
strtype::strtype(char* s)
{
delete[] str;
int i;
for(i=0; s[i]!='\0'; ++i);
str = new char[i+1];
len = i;
strcpy(str, s);
}
void strtype::operator=(char* s)
{
delete[] str;
int i;
for(i=0; s[i]!='\0'; ++i);
str = new char[i+1];
len = i;
strcpy(str, s);
}
bool strtype::operator<(strtype& s)
{
return(strcmp(this->str, s.str) < 0);
}
bool strtype::operator>(strtype& s)
{
return(strcmp(this->str, s.str) > 0);
}
bool strtype::operator==(strtype& s)
{
return(strcmp(this->str, s.str) == 0);
}
strtype strtype::operator+(strtype& s)
{
char str1[100];
strcpy(str1, this->str);
char str2[100];
strcpy(str2, s.str);
strcat(str1, str2);
strtype newString(str1);
return newString;
}
void strtype::display()
{
puts(str);
}
void strtype::get()
{
delete[] str;
char string2[120];
gets(string2);
strtype s(string2);
str = new char[s.len+1];
strcpy(str, s.str);
len = s.len;
}
int main()
{
strtype first, second, third;
int choice;
cout<<"Enter First string: ";
first.get();
cout<<"Enter second string: ";
second.get();
do{
cout<<"\nMENU \n1.Enter string 1 \n2.Enter string 2 \n3.Add strings \n4.Compare Strings \n5.Close \nEnter Choice: ";
cin>>choice;
if(choice == 1)
{
cout<<"Enter First string: ";
first.get();
}
else if(choice == 2)
{
cout<<"Enter Second string: ";
second.get();
}
else if(choice == 3)
{
third = first + second;
third.display();
}
else if(choice == 4)
{
if(first > second) cout<<"First comes first.\n";
if(first == second) cout<<"Both are equal.\n";
if(first < second) cout<<"Second won this time.\n";
}
}while(choice != 5);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment