Here is a code in which i have created for the string class. This article tells how we can implement the functions of a string through making it a class.
Code: Cpp
//Header Files
# include "classfile.h"
/**
* main () this is used to receive the choice of the user and perform the string operations as desired by the user.
*
*/
int main()
{
int choice,length=0,position; //variable declaration.
String s,s1,s2; //object declaration for the class String.
char str[80]; //char array declaration.
while(1){
menu(); //call to the function menu to display the options for the user.
cin>>choice; //to take the input from the user i.e. his choice.
fflush(stdin); //to flush the memory.
switch(choice){
//this is to input a new string from the user and store this string string in the string class.
case STRING_INSERT:
cout<<"\n ENTER A STRING \n ";
gets(str); //inputs a string from the user.
s.Put(str); //calls the function Put to save this string into the string class.
system("cls"); //clears the screen.
Display(s); //displays the stored string,its length and memory allocated for it.
break;
//this is to reverse the string stored in the String class.
case STRING_REVERSE:
s.StrReverse(); //calls the function Reverse to reverse the string stored in the String class.
system("cls"); //clears the screen.
Display(s); //displays the stored string,its length and memory allocated for it.
break;
//this is to remove the starting and ending spaces from the stored string.
case STRING_TRIM:
s.Trim(); //calls the function Trim to remove the spaces from the starting and ending of the string stored in the String class.
system("cls"); //clears the screen.
Display(s); //displays the stored string,its length and memory allocated for it.
break;
//this is to add a new string to the existing string and store it in the class.
case STRING_ADD:
cout<<"\n ENTER A STRING TO ADD TO THE EXISTING ONE \n";
gets(str); //inputs a string from the user to add to the existing string.
s.ConcatString(str);//calls the function ConcatString to add the two strings.
system("cls"); //clears the screen.
Display(s); //displays the stored string,its length and memory allocated for it.
break;
//this is to search where a given substring is present in the stored string and to return its position.
case STRING_SUBSTRING_FIND:
cout<<"\n Enter the string to find \n";
gets(str); //inputs a string from the user to find that substring in the stored string.
position=s.Find(str); //calls the function Find to find the substring in the stored string.
if(position==0){
cout<<"\n the search string does not exist"<<str;
}else {
system("cls"); //clears the screen.
cout<<"\n the search string \t"<<str<<"exists at position \t\t"<<position;
}
break;
//this is to search where a given substring is present in the stored string and to delete it from the string.
case STRING_SUBSTRING_FIND_DELETE:
cout<<"\n Enter the string to delete \n";
gets(str); //inputs a string from the user to find that substring in the stored string and delete it.
position=s.Find(str);//calls the function Find to find the substring in the stored string.
if(position==0){
cout<<"\n the search string does not exist hence cannot be deleted"<<str;
}else {
s.Delete(position,str);//calls the function Delete to delete the substring from the specified position.
system("cls"); //clears the screen.
Display(s); //displays the stored string,its length and memory allocated for it.
}
break;
//this is to add a new string to the existing by use of operator overloading.
case OPERATOR_PLUS_OVERLOADING:
cout<<"\n ENTER A STRING \n ";
gets(str); //inputs a string from the user.
system("cls"); //clears the screen.
cout<<"\n operator overloading for single object";
s=s+str; //calls the overloaded operator + for addition of a string to the same string object.
Display(s); //displays the stored string,its length and memory allocated for it.
cout<<"\n operator overloading for different objects";
s1=str;
s2=s+s1; //calls the overloaded operator + for addition of a string object to the previous string object and stores into a new string object.
Display(s2); //displays the stored string,its length and memory allocated for it.
break;
//this is to delete a sub string from a string by use of operator overloading.
case OPERATOR_MINUS_OVERLOADING:
cout<<"\n Enter the string to delete \n";
gets(str); //inputs a string from the user which is to be deleted.
system("cls"); //clears the screen.
cout<<"\n operator overloading for single object";
s=s-str; //calls the overloaded operator - to delete a sub string from the same string object.
Display(s); //displays the stored string,its length and memory allocated for it.
cout<<"\n operator overloading for different objects";
s=s-s1; //calls the overloaded operator - to delete a new string object from the previous string object and stores into the previous string object.
Display(s); //displays the stored string,its length and memory allocated for it.
break;
//this is to enter a new string by use of the operator overloading.
case OPERATOR_EQUALS_OVERLOADING:
cout<<"\n Enter the String \n";
gets(str); //inputs a string from the user.
system("cls"); //clears the screen.
cout<<"overloading operator for string assignment";
s=str; //calls the overloaded operator = to assign the string to the s string object.
Display(s); //displays the stored string,its length and memory allocated for it.
cout<<"after overloading for string object";
s2=s; //calls the overloaded operator = to assign the s string object to s2 string object.
Display(s2); //displays the stored string,its length and memory allocated for it.
break;
//this is to display the details of the class members.
case DISPLAY:
system("cls"); //clears the screen.
Display(s); //displays the stored string,its length and memory allocated for it.
break;
//this is to exit from the application.
case EXIT:
s.Free(); //used to free the memory allocated for string object s.
s1.Free(); //used to free the memory allocated for string object s1.
s2.Free(); //used to free the memory allocated for string object s2.
return 0; //exists from the application.
}
}
_getch();
}
/**
* String(void) () is the constructor for the class
*
* @note
* <ul>
* <li>it allocates some values to the private members of the class.
* </ul>
*
*/
String::String(void)
{
vString=NULL; //initializes the string.
vLength=0; //initializes the length of the string with 0.
vSize=0; //initializes the size of the string with 0.
}
/**
* String(void) () is the destructor for the class
*
* @note
* <ul>
* <li>it frees the memory allocated for the members of the class.
* </ul>
*
*/
String::~String(void)
{
Free();
}
/**
* Put () used to receive a new string from the user
*
*/
void String::Put( char* pString )
{
unsigned int length=0;
length=StringLength(pString);
if( vLength==0 || length > vLength){
vString=(char*)calloc((StringLength(pString) + 1),sizeof(char)); //allocates memory for the string.
}
StrCpy(vString,pString); //calls the function StrCpy to copy the string.
vLength=length; //to calculate the length of the string stored in the class.
vSize=vLength+1; //to calculate the size of the string.
}
/**
* Display() used to display the string stored in the class.
*
* @note
* <ul>
* <li>it also displays the length of the string stored.
* <li>it also displays the memory allocated for the string to be stored.
* </ul>
*
*/
void Display(String& pString)
{
cout<<"\n The String Is "<<pString.vString; //displays the string stored in the class.
cout<<"\n The Length Of the String Is:- "<<pString.vLength; //displays the length of the stored string.
cout<<"\n The Size Of The String Is:- "<<pString.vSize; //displays the size of the stored string.
}
/**
* StrReverse () used to reverse the string stored in the class
*
*/
void String::StrReverse()
{
char c; //variable initialization.
char *tail,*head; //pointer to a char initializations.
if(vString!=NULL)
{
head=vString; //allocates the starting address of the string to head.
tail=vString+StringLength(vString)-1; //allocates the ending address of the string to head.
//used to reverse the string .
while( head < tail ) {
c = *tail;
*(tail--) = *head;
*(head++) = c;
}
}else{
cout<<"REVERSING NOT POSSIBLE.N0 STRING PRESENT";
}
}
/**
* Trim () used to remove the spaces at the starting and ending of the string of the class.
*
* @note
* <ul>
* <li>it makes use of the lTrim to remove the starting spaces.
* <li>it makes use of the rTrim to remove the ending spaces.
* </ul>
*
*/
void String::Trim()
{
//checks whether there is a space at the starting or not ,if present calls Ltrim.
if(vString[0]==char(32))
Ltrim(vString); //calls the Ltrim function to remove the starting spaces.
//checks whether there is a space at the end or not ,if present calls Rtrim.
if(vString[vLength-1]==char(32))
Rtrim(vString); //calls the Rtrim function to remove the ending spaces.
vLength=StringLength(vString); //calls the function StringLength to calculate the length of the string.
vSize=vLength+1; //to calculate the size of the string.
}
/**
* ConcatString () used to add a string into an existing string.
*
*/
void String:: ConcatString(char *pString)
{
char str[80]; //variable initialisation.
int length; //variable initialisation.
int len; //variable initialisation.
len=StringLength(vString); //calls the function StringLength to calculate the length of the string.
StrCpy(str,vString); //calls the StrCpy function to copy the string.
length=len+StringLength(pString);//calls the function StringLength to calculate the total length of the string.
(char*)realloc(vString,(length+1)*sizeof(char)); //used to reallocate the memory for the string.
StrCpy(vString,str); //calls the StrCpy function to copy the string.
//adds the new string at the end of the old string.
while(*pString!=NULL)
{
vString[len++]=*pString;
pString++;
}
vString[len]='\0'; //adds a terminating null to the new string.
vLength=length; //used to get the length of the string stored.
vSize=length+1; //used to get the size of the string stored.
}
/**
* Find() used to find a substring of the string.
*
*/
int String::Find(char *pString)
{
int plength,vLength,k,count=0,i; //variable initialization.
plength=StringLength(pString); //calls the function StringLength to calculate the length of the string pLength.
vLength=StringLength(vString); //calls the function StringLength to calculate the length of the string vLength.
//checks whether the substring length is greater than the original string length.
if(plength>vLength)
return 0;
//checks whether the substring is equal to the original string if their lengths are same.
if(plength==vLength && strcmp(pString,vString)==0)
return 1;
//checks whether the substring is present in the original string when substring length is smaller than the original string length.
for(k=0;k<=vLength;k++){
for(i=0;i<plength;i++){
if(vString[k+i]!=pString[i]){
break;
}else{
count++;
}if(count==plength){
return k+1;
break;
}
}
}
return 0;
}
/**
* Delete () used to delete a part of the string stored in the class.
*
* @note
* <ul>
* <li>it searches a substring of the string and if found it deletes it.
* <ul>
*
*/
void String::Delete(int pPos,char *pString)
{
int plength; //variable initialization.
plength=StringLength(pString); //calls the function StringLength to calculate the length of the string pLength.
//checks whether the position is less than or equal to the length of the stored string.
while(pPos<=StringLength(vString)){
vString[pPos-1]=vString[pPos+plength-1]; //shifts the characters of the string to delete the substring.
pPos++; //increments the position.
}
vLength=StringLength(vString); //calls the function StringLength to calculate the length of the string vLength.
vSize=vLength+1; //to calculate the size of the string.
}
/**
* Get() used to retrieve the string stored in the class.
*
*/
char* String::Get ( void )
{
return vString; //returns the string stored in the String class.
}
/**
* Free() used to free the memory allocated for the string stored in the class.
*
*/
void String::Free()
{
vString=NULL;
free(vString);
}
/**
* String& operator+ () used to overload the operator + for adding a new string to the existing string.
*
*/
String& String::operator+(char* pStr)
{
int len; //variable initialisation.
len=StringLength(pStr)+vLength+1; //calculates the total length of the two strings.
//String *result=new String[]; //allocates memory for the string class.
//this->vString=new char[len+1]; //allocates memory for the variable result.
//StrCpy(result->vString, vString); //calls the StrCpy function to copy the string.
this->ConcatString(pStr); //calls the ConcatString function to concat the string.
this->vLength=StringLength(this->vString); //calls the function StringLength to calculate the length of the string vLength.
this->vSize=this->vLength+1; //to calculate the size of the string.
return *this;
}
/**
* String& operator- () used to overload the operator - for deletion of a string.
*
*/
String& String::operator-(char* pStr)
{
int position,len; //variable initialisation.
String *result=new String[]; //allocates memory for the string class.
len=StringLength(vString); //calls the function StringLength to calculate the length of the string vLength.
result->vString=new char[len+1]; //allocates memory for the variable result.
StrCpy(result->vString,vString); //calls the StrCpy function to copy the string.
position=result->Find(pStr); //calls the function find to find the position of occurence of substring.
//calls the delete function to delete the substring at the specified position.
if(position!=0){
result->Delete(position,pStr);
}else{
cout<<"\n deletion not possible";
}
return *result; //returns the new string to be stored in the String class.
}
/**
* String& operator= () used to overload the operator = for assignment of a new string.
*
*/
String& String::operator=(char* pStr)
{
this->Put(pStr); //calls the Put function to receive a completely new string.
return *this; //we retutn the string to store it in the String class.
}
/**
* String& operator+ () used to overload the operator + for adding a new string object to the existing string object.
*
*/
String& String::operator+(String& pStr)
{
String *temp;
temp=new String[];
temp->vString=new char[vLength+1]; //allocates memory for the variable temp.
StrCpy(temp->vString,vString); //calls the StrCpy function to copy the string.
temp->ConcatString(pStr.vString); //calls the ConcatString function to concat the string.
return *temp; //we return the string to store it in the String class.
}
/**
* String& operator- () used to overload the operator - for deletion of a string object.
*
*/
String& String::operator-(String& pStr)
{
int position,len; //variable initialisation.
String *result=new String[]; //allocates memory for the string class.
len=StringLength(vString); //calls the function StringLength to calculate the length of the string vLength.
result->vString=new char[len+1]; //allocates memory for the variable result.
StrCpy(result->vString,vString); //calls the StrCpy function to copy the string.
position=result->Find(pStr.vString); //calls the function find to find the position of occurence of substring.
//calls the delete function to delete the substring at the specified position.
if(position!=0){
result->Delete(position,pStr.vString);
}else{
cout<<"\n deletion not possible";
}
return *result;
}
/**
* String& operator= () used to overload the operator = for assignment of a new string object.
*
*/
String& String::operator=(String& pStr)
{
this->Put(pStr.vString); //calls the Put function to receive a completely new string.
return *this; //we retutn the string to store it in the String class.
}
/**
* String(const String& Str) used to implement the working of the copy constructor.
*
*/
String::String(const String& pStr)
{
int len; //variable initialisation.
len=StringLength(pStr.vString); //calls the function StringLength to calculate the length of the string vLength.
this->vString = new char[len+1]; //allocates memory .
StrCpy(this->vString,vString); //calls the StrCpy function to copy the string.
}
/**
* menu() used to create the menu.
*
* @note
* <ul>
* <li> this function is called whenever the menu is to be displyed.
* <ul>
*
*/
void menu()
{
cout<<"\n ENTER THE CHOICE FROM THE BELOW LIST\n\n\t"
<<"\n *****************************************************************************\n\n\t"
<<"\n\n 1 to insert any new string"
<<"\n\n 2 to reverse the existing string"
<<"\n\n 3 to trim the existing string from both sides i.e. left and right"
<<"\n\n 4 to add another string to the existing string"
<<"\n\n 5 to find particular string in the existing string"
<<"\n\n 6 to find and remove a particular string from within the existing string"
<<"\n\n 7 to overload the operator +"
<<"\n\n 8 to overload the operator -"
<<"\n\n 9 to overload the operator ="
<<"\n\n 10 to display the stored string"
<<"\n\n 11 to terminate application "
<<"\n\n ****************************************************************************\n\n"
<<"My Choice Is: ";
}
/**
* StringLength () used to find the length of the string.
*
* @note
* <ul>
* <li>it receives the string whose length is to be found.
* </ul>
*
*/
int StringLength(char* pString)
{
int length=0; //variable initialisation.
//calculates the no of characters in the string until a NULL is encountered.
while(*pString!='\0'){
pString++;
length++;
}
return length; //returns the length of the string.
}
/**
* rTrim () used to remove all the spaces at the end of the String.
*
* @note
* <ul>
* <li>it receives the string whose end spaces are to be removed from the trim function.
* </ul>
*
*/
void Rtrim(char *pString)
{
int length; //variable initialisation.
length=StringLength(pString); //calls the function StringLength to calculate the length of the string.
//checks for the spaces from the end of the string and removes them accoringly.
while(length>0)
{
if(*(pString+(--length))==char(32))
{
}
else
{
break;
}
}
*(pString+length+1)='\0'; //allocates a null at the point before which a character other than space is found.
}
/**
* lTrim () used to remove all the spaces at the starting of the String.
*
* @note
* <ul>
* <li>it receives the string whose starting spaces are to be removed from the trim function.
* </ul>
*
*/
void Ltrim(char *pString)
{
int index=0,i; //variable initialisation.
//checks for the spaces from the starting of the string and counts the number.
while(pString!=NULL)
{
if(*(pString+index)==char(32))
{
index++;
}
else
{
break;
}
}
//removes teh spaces by shifting the string.
for(i=0;i<StringLength(pString);i++)
{
pString[i]=pString[i+index];
}
}
/**
* StrCpy () used to display copy a given string into another.
*
*/
void StrCpy(char *pSource, char *pTarget)
{
//copies char by char from one loction to another.
while (*pSource++ = *pTarget++);
}