String Class Implementation in C C++ Programming Language with menu string compare string equal string upper case string lower case
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
const int MAX=80;
class MyString
{ private:
char str[MAX];
public:
void init()
{
str[0]=NULL;
}
void input()
{
cin.ignore(10,'\n');
cin.get(str,MAX);
}
void display()
{
cout<<str;
}
int strlength();
void strUpper();
void strLower();
int strSearch(MyString);
void strDelete(MyString);
int strCompare(MyString);
int strIcompare(MyString);
void strConcate(MyString);
};
int MyString::strlength()
{
int l=0;
while(str[l]!='\0')
l++;
return l;
}
void MyString::strUpper()
{
int i=0;
while(str[i]!=NULL)
{
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
i++;
}
}
void MyString::strLower()
{
int i=0;
while(str[i]!=NULL)
{
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]+32;
i++;
}
}
int MyString::strSearch(MyString s)
{
int i=0,l2;
l2=s.strlength();
while(str[i]!='\0')
if(str[i++]!= s.str[0])
continue;
else
{
for(int j=1,k=i;j<l2;j++,i++)
if(str[i] != s.str[j])
break;
if(j==l2)
{
return k;//k returned for deletion purpose
}
else
i=k;
}
return -1;
}
void MyString::strDelete(MyString s)
{
int i,l1,l2,j,k,len,end,start;
l1=strlength();
l2=s.strlength();
for(i=0;i<l1;i++)
{
len=0;
if(str[i]==s.str[0])
{
for(j=i,k=0;k<l2;j++,k++)
{
if(str[j]==s.str[k])
{
len++;
end=j;
}
}
if(len==l2)
{
start=end-l2+1;
for(i=start,j=end+1;j<=l1;i++,j++)
str[i]=str[j];
}
}
}
}
int MyString::strCompare(MyString s)
{
int l1,i,temp=0,found;
char ch[MAX];
l1=strlength();
for(i=0;i<l1;i++)
{
if(str[i]==s.str[i])
temp++;
if(str[i]>s.str[i])
return -1;
if(str[i]<s.str[i])
return 1;
}
if(temp==l1)
return 0;
else
return 2;
}
int MyString::strIcompare(MyString s)
{
int l1,i,temp,found;
s.strUpper();
l1=strlength();
for(i=0;i<l1;i++)
{
if(str[i]==s.str[i])
temp++;
if(str[i]>s.str[i])
return -1;
if(str[i]<s.str[i])
return 1;
}
if(temp==l1)
return 0;
else
return 2;
}
void MyString::strConcate(MyString s)
{
int l1,l2,i,j,sum;
l1=strlength();
l2=s.strlength();
sum=l1+l2;
if(sum<80)
{
for(i=l1,j=0;j<l2+1;i++,j++)
str[i]=s.str[j];
}
else
cout<<"OVERLOAD STRING";
}
void opermenu();
void stringmenu();
/********************************MAIN FUNCTION*****************************/
void main()
{
MyString S1,S2,temp;
int len,r,num,chmain,chstring,choper;
clrscr();
S1.init();
S2.init();
do
{
textcolor(3);
clrscr();
gotoxy(22,10);
cout<<"<*><*><*>MYSTRING MAIN MENU<*><*><*>";
gotoxy(30,14);
cout<<"1:STRING NO 1.";
gotoxy(30,16);
cout<<"2:STRING NO 2.";
gotoxy(30,18);
cout<<"3:OPERATION B/W STRINGS.";
gotoxy(30,20);
cout<<"4:EXIT.";
gotoxy(25,24);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,24);
cin>>chmain;
switch(chmain)
{
case 1:
do
{
textcolor(6);
clrscr();
stringmenu();
gotoxy(25,32);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,32);
cin>>chstring;
switch(chstring)
{
case 1:
clrscr();
gotoxy(25,2);
cout<<"**** ENTER STRING ****"<<endl;
S1.input();
getch();
break;
case 2:
clrscr();
gotoxy(25,2);
cout<<"**** OUTPUT STRING ****"<<endl;
S1.display();
getch();
break;
case 3:
clrscr();
gotoxy(25,15);
cout<<"**** STRING LENGTH ****";
gotoxy(28,18);
cout<<"TOTAL CHARACTER : "<<S1.strlength()-1;
getch();
break;
case 4:
clrscr();
gotoxy(25,15);
cout<<"**** STRING DELETE ****";
gotoxy(18,20);
cout<<"ENTER CHARACTER YOU WANT TO DELETE : ";
temp.input();
r=S1.strSearch(temp);
if(r==-1)
{
gotoxy(28,22);
cout<<"* STRING NOT FOUND ! *";
getch();
}
else
{
gotoxy(21,22);
cout<<"DO YOU WANT TO DELETE CHARACTER";
gotoxy(21,24);
cout<<"Y=YES";
gotoxy(48,24);
cout<<"N=NO";
char ch=getch();
if(ch=='y'||ch=='Y')
{
S1.strDelete(temp);
gotoxy(24,27);
cout<<"** CHARACTER IS DELETE **";
getch();
}
}
break;
case 5:
clrscr();
gotoxy(25,15);
cout<<"**** STRING SEARCH ****";
gotoxy(20,18);
cout<<"ENTER CHARACTER YOU WANT TO SEARCH : ";
temp.input();
r=S1.strSearch(temp);
if(r==-1)
{
gotoxy(28,20);
cout<<"STRING NOT FOUND";
getch();
}
else
{
gotoxy(25,20);
cout<<"STRING FOUND AT "<<r<<" PLACE";
getch();
}
break;
case 6:
clrscr();
S1.strLower();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO LOWER CASE *>>>";
getch();
break;
case 7:
clrscr();
S1.strUpper();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO UPPER CASE *>>>";
getch();
break;
case 8:
chmain=1;
break;
default:
gotoxy(24,34);
cout<<"INVALID CHOICE PLEASE REENTER";
chstring=1;
getch();
}
}while(chstring<8);
break;
case 2:
do
{
textcolor(6);
clrscr();
stringmenu();
gotoxy(25,32);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,32);
cin>>chstring;
switch(chstring)
{
case 1:
clrscr();
gotoxy(25,2);
cout<<"**** ENTER STRING ****"<<endl;
S2.input();
getch();
break;
case 2:
clrscr();
gotoxy(25,2);
cout<<"**** OUTPUT STRING ****"<<endl;
S2.display();
getch();
break;
case 3:
clrscr();
gotoxy(25,15);
cout<<"**** STRING LENGTH ****";
gotoxy(28,18);
cout<<"TOTAL CHARACTER : "<<S2.strlength()-1;
getch();
break;
case 4:
clrscr();
gotoxy(25,15);
cout<<"**** STRING DELETE ****";
gotoxy(18,20);
cout<<"ENTER CHARACTER YOU WANT TO DELETE : ";
temp.input();
r=S2.strSearch(temp);
if(r==-1)
{
gotoxy(28,22);
cout<<"* STRING NOT FOUND ! *";
getch();
}
else
{
gotoxy(21,22);
cout<<"DO YOU WANT TO DELETE CHARACTER";
gotoxy(21,24);
cout<<"Y=YES";
gotoxy(48,24);
cout<<"N=NO";
char ch=getch();
if(ch=='y'||ch=='Y')
{
S2.strDelete(temp);
gotoxy(24,27);
cout<<"** CHARACTER IS DELETE **";
getch();
}
}
break;
case 5:
clrscr();
gotoxy(25,15);
cout<<"**** STRING SEARCH ****";
gotoxy(20,18);
cout<<"ENTER CHARACTER YOU WANT TO SEARCH : ";
temp.input();
r=S2.strSearch(temp);
if(r==-1)
{
gotoxy(28,20);
cout<<"STRING NOT FOUND";
getch();
}
else
{
gotoxy(25,20);
cout<<"STRING FOUND AT "<<r<<" PLACE";
getch();
}
break;
case 6:
clrscr();
S2.strLower();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO LOWER CASE *>>>";
getch();
break;
case 7:
clrscr();
S2.strUpper();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO UPPER CASE *>>>";
getch();
break;
case 8:
chmain=1;
break;
default:
gotoxy(24,34);
cout<<"INVALID CHOICE PLEASE REENTER";
chstring=1;
getch();
}
}while(chstring<8);
break;
case 3:
do
{
textcolor(6);
clrscr();
opermenu();
gotoxy(25,24);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,24);
cin>>choper;
switch(choper)
{
case 1:
clrscr();
r=S1.strCompare(S2);
if(r==0)
{
gotoxy(20,20);
cout<<"STRING # 1 IS EQUAL TO STRING # 2";
}
if(r==1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS LESS THAN STRING # 2";
}
if(r==-1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS GREATER THAN STRING # 2";
}
getch();
break;
case 2:
clrscr();
r=S1.strIcompare(S2);
if(r==0)
{
gotoxy(20,20);
cout<<"STRING # 1 IS EQUAL TO STRING # 2";
}
if(r==1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS LESS THAN STRING # 2";
}
if(r==-1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS GREATER THAN STRING # 2";
}
getch();
break;
case 3:
clrscr();
S1.strConcate(S2);
gotoxy(2,2);
cout<<"**** STRING CONCATE ****";
gotoxy(1,3);
S1.display();
getch();
break;
case 4:
chmain=1;
break;
default:
gotoxy(25,26);
cout<<"INVALID CHOICE PLEASE REENTER";
choper=1;
getch();
}
}while(choper>4);
break;
case 4:
exit(0);
break;
default:
gotoxy(24,28);
cout<<"INVALID CHOICE PLEASE REENTER !";
chmain=1;
getch();
}//switch main end
}while(chmain<4);
getch();
}
void stringmenu()
{
clrscr();
textcolor(6);
gotoxy(22,10);
cout<<"<*><*><*>STRING NO 1 MENU<*><*><*>";
gotoxy(30,14);
cout<<"1:INPUT STRING.";
gotoxy(30,16);
cout<<"2:OUTPUT STRING.";
gotoxy(30,18);
cout<<"3:STRING LENGTH.";
gotoxy(30,20);
cout<<"4:DELETE STRING.";
gotoxy(30,22);
cout<<"5:SEARCH STRING.";
gotoxy(30,24);
cout<<"6:CONVERT LOWER CASE.";
gotoxy(30,26);
cout<<"7:CONVERT UPPER CASE.";
gotoxy(30,28);
cout<<"8:GO TO MAIN MENU.";
}
void opermenu()
{
textcolor(6);
gotoxy(24,10);
cout<<"<*><*><*>OPERATION MENU<*><*><*>";
gotoxy(30,14);
cout<<"1:STRING COMPARE.";
gotoxy(30,16);
cout<<"2:STRING ICOMPARE.";
gotoxy(30,18);
cout<<"3:STRING CONCATE.";
gotoxy(30,20);
cout<<"4:GO TO MAIN MENU.";
}
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
const int MAX=80;
class MyString
{ private:
char str[MAX];
public:
void init()
{
str[0]=NULL;
}
void input()
{
cin.ignore(10,'\n');
cin.get(str,MAX);
}
void display()
{
cout<<str;
}
int strlength();
void strUpper();
void strLower();
int strSearch(MyString);
void strDelete(MyString);
int strCompare(MyString);
int strIcompare(MyString);
void strConcate(MyString);
};
int MyString::strlength()
{
int l=0;
while(str[l]!='\0')
l++;
return l;
}
void MyString::strUpper()
{
int i=0;
while(str[i]!=NULL)
{
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
i++;
}
}
void MyString::strLower()
{
int i=0;
while(str[i]!=NULL)
{
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]+32;
i++;
}
}
int MyString::strSearch(MyString s)
{
int i=0,l2;
l2=s.strlength();
while(str[i]!='\0')
if(str[i++]!= s.str[0])
continue;
else
{
for(int j=1,k=i;j<l2;j++,i++)
if(str[i] != s.str[j])
break;
if(j==l2)
{
return k;//k returned for deletion purpose
}
else
i=k;
}
return -1;
}
void MyString::strDelete(MyString s)
{
int i,l1,l2,j,k,len,end,start;
l1=strlength();
l2=s.strlength();
for(i=0;i<l1;i++)
{
len=0;
if(str[i]==s.str[0])
{
for(j=i,k=0;k<l2;j++,k++)
{
if(str[j]==s.str[k])
{
len++;
end=j;
}
}
if(len==l2)
{
start=end-l2+1;
for(i=start,j=end+1;j<=l1;i++,j++)
str[i]=str[j];
}
}
}
}
int MyString::strCompare(MyString s)
{
int l1,i,temp=0,found;
char ch[MAX];
l1=strlength();
for(i=0;i<l1;i++)
{
if(str[i]==s.str[i])
temp++;
if(str[i]>s.str[i])
return -1;
if(str[i]<s.str[i])
return 1;
}
if(temp==l1)
return 0;
else
return 2;
}
int MyString::strIcompare(MyString s)
{
int l1,i,temp,found;
s.strUpper();
l1=strlength();
for(i=0;i<l1;i++)
{
if(str[i]==s.str[i])
temp++;
if(str[i]>s.str[i])
return -1;
if(str[i]<s.str[i])
return 1;
}
if(temp==l1)
return 0;
else
return 2;
}
void MyString::strConcate(MyString s)
{
int l1,l2,i,j,sum;
l1=strlength();
l2=s.strlength();
sum=l1+l2;
if(sum<80)
{
for(i=l1,j=0;j<l2+1;i++,j++)
str[i]=s.str[j];
}
else
cout<<"OVERLOAD STRING";
}
void opermenu();
void stringmenu();
/********************************MAIN FUNCTION*****************************/
void main()
{
MyString S1,S2,temp;
int len,r,num,chmain,chstring,choper;
clrscr();
S1.init();
S2.init();
do
{
textcolor(3);
clrscr();
gotoxy(22,10);
cout<<"<*><*><*>MYSTRING MAIN MENU<*><*><*>";
gotoxy(30,14);
cout<<"1:STRING NO 1.";
gotoxy(30,16);
cout<<"2:STRING NO 2.";
gotoxy(30,18);
cout<<"3:OPERATION B/W STRINGS.";
gotoxy(30,20);
cout<<"4:EXIT.";
gotoxy(25,24);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,24);
cin>>chmain;
switch(chmain)
{
case 1:
do
{
textcolor(6);
clrscr();
stringmenu();
gotoxy(25,32);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,32);
cin>>chstring;
switch(chstring)
{
case 1:
clrscr();
gotoxy(25,2);
cout<<"**** ENTER STRING ****"<<endl;
S1.input();
getch();
break;
case 2:
clrscr();
gotoxy(25,2);
cout<<"**** OUTPUT STRING ****"<<endl;
S1.display();
getch();
break;
case 3:
clrscr();
gotoxy(25,15);
cout<<"**** STRING LENGTH ****";
gotoxy(28,18);
cout<<"TOTAL CHARACTER : "<<S1.strlength()-1;
getch();
break;
case 4:
clrscr();
gotoxy(25,15);
cout<<"**** STRING DELETE ****";
gotoxy(18,20);
cout<<"ENTER CHARACTER YOU WANT TO DELETE : ";
temp.input();
r=S1.strSearch(temp);
if(r==-1)
{
gotoxy(28,22);
cout<<"* STRING NOT FOUND ! *";
getch();
}
else
{
gotoxy(21,22);
cout<<"DO YOU WANT TO DELETE CHARACTER";
gotoxy(21,24);
cout<<"Y=YES";
gotoxy(48,24);
cout<<"N=NO";
char ch=getch();
if(ch=='y'||ch=='Y')
{
S1.strDelete(temp);
gotoxy(24,27);
cout<<"** CHARACTER IS DELETE **";
getch();
}
}
break;
case 5:
clrscr();
gotoxy(25,15);
cout<<"**** STRING SEARCH ****";
gotoxy(20,18);
cout<<"ENTER CHARACTER YOU WANT TO SEARCH : ";
temp.input();
r=S1.strSearch(temp);
if(r==-1)
{
gotoxy(28,20);
cout<<"STRING NOT FOUND";
getch();
}
else
{
gotoxy(25,20);
cout<<"STRING FOUND AT "<<r<<" PLACE";
getch();
}
break;
case 6:
clrscr();
S1.strLower();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO LOWER CASE *>>>";
getch();
break;
case 7:
clrscr();
S1.strUpper();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO UPPER CASE *>>>";
getch();
break;
case 8:
chmain=1;
break;
default:
gotoxy(24,34);
cout<<"INVALID CHOICE PLEASE REENTER";
chstring=1;
getch();
}
}while(chstring<8);
break;
case 2:
do
{
textcolor(6);
clrscr();
stringmenu();
gotoxy(25,32);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,32);
cin>>chstring;
switch(chstring)
{
case 1:
clrscr();
gotoxy(25,2);
cout<<"**** ENTER STRING ****"<<endl;
S2.input();
getch();
break;
case 2:
clrscr();
gotoxy(25,2);
cout<<"**** OUTPUT STRING ****"<<endl;
S2.display();
getch();
break;
case 3:
clrscr();
gotoxy(25,15);
cout<<"**** STRING LENGTH ****";
gotoxy(28,18);
cout<<"TOTAL CHARACTER : "<<S2.strlength()-1;
getch();
break;
case 4:
clrscr();
gotoxy(25,15);
cout<<"**** STRING DELETE ****";
gotoxy(18,20);
cout<<"ENTER CHARACTER YOU WANT TO DELETE : ";
temp.input();
r=S2.strSearch(temp);
if(r==-1)
{
gotoxy(28,22);
cout<<"* STRING NOT FOUND ! *";
getch();
}
else
{
gotoxy(21,22);
cout<<"DO YOU WANT TO DELETE CHARACTER";
gotoxy(21,24);
cout<<"Y=YES";
gotoxy(48,24);
cout<<"N=NO";
char ch=getch();
if(ch=='y'||ch=='Y')
{
S2.strDelete(temp);
gotoxy(24,27);
cout<<"** CHARACTER IS DELETE **";
getch();
}
}
break;
case 5:
clrscr();
gotoxy(25,15);
cout<<"**** STRING SEARCH ****";
gotoxy(20,18);
cout<<"ENTER CHARACTER YOU WANT TO SEARCH : ";
temp.input();
r=S2.strSearch(temp);
if(r==-1)
{
gotoxy(28,20);
cout<<"STRING NOT FOUND";
getch();
}
else
{
gotoxy(25,20);
cout<<"STRING FOUND AT "<<r<<" PLACE";
getch();
}
break;
case 6:
clrscr();
S2.strLower();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO LOWER CASE *>>>";
getch();
break;
case 7:
clrscr();
S2.strUpper();
gotoxy(20,20);
cout<<"<<<* CHRACTER CONVERT INTO UPPER CASE *>>>";
getch();
break;
case 8:
chmain=1;
break;
default:
gotoxy(24,34);
cout<<"INVALID CHOICE PLEASE REENTER";
chstring=1;
getch();
}
}while(chstring<8);
break;
case 3:
do
{
textcolor(6);
clrscr();
opermenu();
gotoxy(25,24);
cout<<"ENTER CHOICE FOR MENU:[ ]";
gotoxy(49,24);
cin>>choper;
switch(choper)
{
case 1:
clrscr();
r=S1.strCompare(S2);
if(r==0)
{
gotoxy(20,20);
cout<<"STRING # 1 IS EQUAL TO STRING # 2";
}
if(r==1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS LESS THAN STRING # 2";
}
if(r==-1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS GREATER THAN STRING # 2";
}
getch();
break;
case 2:
clrscr();
r=S1.strIcompare(S2);
if(r==0)
{
gotoxy(20,20);
cout<<"STRING # 1 IS EQUAL TO STRING # 2";
}
if(r==1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS LESS THAN STRING # 2";
}
if(r==-1)
{
gotoxy(20,20);
cout<<"STRING # 1 IS GREATER THAN STRING # 2";
}
getch();
break;
case 3:
clrscr();
S1.strConcate(S2);
gotoxy(2,2);
cout<<"**** STRING CONCATE ****";
gotoxy(1,3);
S1.display();
getch();
break;
case 4:
chmain=1;
break;
default:
gotoxy(25,26);
cout<<"INVALID CHOICE PLEASE REENTER";
choper=1;
getch();
}
}while(choper>4);
break;
case 4:
exit(0);
break;
default:
gotoxy(24,28);
cout<<"INVALID CHOICE PLEASE REENTER !";
chmain=1;
getch();
}//switch main end
}while(chmain<4);
getch();
}
void stringmenu()
{
clrscr();
textcolor(6);
gotoxy(22,10);
cout<<"<*><*><*>STRING NO 1 MENU<*><*><*>";
gotoxy(30,14);
cout<<"1:INPUT STRING.";
gotoxy(30,16);
cout<<"2:OUTPUT STRING.";
gotoxy(30,18);
cout<<"3:STRING LENGTH.";
gotoxy(30,20);
cout<<"4:DELETE STRING.";
gotoxy(30,22);
cout<<"5:SEARCH STRING.";
gotoxy(30,24);
cout<<"6:CONVERT LOWER CASE.";
gotoxy(30,26);
cout<<"7:CONVERT UPPER CASE.";
gotoxy(30,28);
cout<<"8:GO TO MAIN MENU.";
}
void opermenu()
{
textcolor(6);
gotoxy(24,10);
cout<<"<*><*><*>OPERATION MENU<*><*><*>";
gotoxy(30,14);
cout<<"1:STRING COMPARE.";
gotoxy(30,16);
cout<<"2:STRING ICOMPARE.";
gotoxy(30,18);
cout<<"3:STRING CONCATE.";
gotoxy(30,20);
cout<<"4:GO TO MAIN MENU.";
}
0 comments:
Post a Comment