Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Oct 13, 2015

A Simple String Class for Beginners

The code in String.cpp can be used to do basic operations based on character array. It can also be used to see some of the extra flavors of Object Oriented programming.

A few properties about the code include:

  1. I use Constructor & Destructor. How a constructor and destructor can be used using C++ is shown.
  2. You can see the concept of class.
  3. The code is menu-driven, so you can see the concept of switch-case is shown.
  4. Function calling is another important property illustrated in this code. You can see how to call a function by both Call by value and Call by reference.
  5. Polymorphism is another property that you can see is achieved in this code.
  6. This code also calculates Iteration number for each function. It gives proper result including approximated iteration number.
  7. Some basic functions are shown. Such as:
    1. to take the input
    2. to display the input
    3. to calculate the length of the string, i.e character array
    4. to reverse the given input string
    5. the code can judge, if the given string is palindrome or not.
    6. to abbreviate the string
    7. this code can concatenate between two given strings
    8. it also can compare between two given strings ( I make it case in-sensitive )
    9. It can convert the given string in Upper-Lower case. ( for example, Arup Kr Goswami )
    10. It can search a small sub-string from the given input string, if exist.
    11. It also can find out sub-string from the given input string.
    12. To stop the running program, Exit function is also used.
  8. Pointers are also used in this code, so you'll be able to see the concepts around pointers.

I hope the code in String.cpp will be useful for the beginners:

The Code:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

//class definition 
class MyString		
{
 private:
  char A[40];

//prototype declaration
 public:		
  void menu(int*);
  void Input(void);  
  int Input(char[]);   
  int Display(char[]);
  int Display(void);
  int Length(int*,int*);
  int Length(int*,char[],int*);
  int Reverse(int,char[],int*);
  int Palindrome(int*,int*);
  void Concatenate(char[],char[],int*);
  int Compare(char[],int*,int*);
  void Abbreviate(char[],int*);
  void UpperLower(char[],int*);
  void Search(char[],int*,int,int,int*);
  void SubString(int*,int*,char[],int*);
  void Change(char B[],int*);
  void Change(void);  

//Constructor
  MyString(void)		
  {
    system("clear");
    cout<<"\n\n\n\n\n\n\n\n\t\t\t\t\tSoftware initialising...\n\t\t\t\t\tPreparing for First time use...\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\tPress Enter key...\n\n";
    getchar();
    system("clear");
    cout<<"\n\n\n\n\n\n\n\n\t\t\t\t\tThis is the Demonstration of MyString Class...\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\tPress Enter key...\n\n";
    getchar();
    system("clear");
    Input();
  }

//Destructor
 ~MyString(void)		
  {
    system("clear");
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t\t\t\tSystem is shutting down...\n\n\t\t\t\t\tGood Bye...\n\n\n\n\n\n\n\n\n\n\n\n";
  }
};

//menu function
void MyString::menu(int *choice)
{
  cout<<"\n";
  cout<<"\t\t\t\t   +-------------+\n";
  cout<<"\t\t\t\t   |  MyString   |\n";
  cout<<"\t\t\t\t   +-------------+\n\n\n";
  cout<<"\t\t\t\t+------+------------+\n";
  cout<<"\t\t\t\t|OPTION| DETAILS    |\n";
  cout<<"\t\t\t\t+------+------------+\n";
  cout<<"\t\t\t\t|   99.| Exit       |\n";
  cout<<"\t\t\t\t|    1.| Input      |\n";
  cout<<"\t\t\t\t|    2.| Display    |\n";
  cout<<"\t\t\t\t|    3.| Reverse    |\n";
  cout<<"\t\t\t\t|    4.| Palindrome |\n";
  cout<<"\t\t\t\t|    5.| Length     |\n";
  cout<<"\t\t\t\t|    6.| Compare    |\n";
  cout<<"\t\t\t\t|    7.| Concatenate|\n";
  cout<<"\t\t\t\t|    8.| Abbreviate |\n";
  cout<<"\t\t\t\t|    9.| UpperLower |\n";
  cout<<"\t\t\t\t|   10.| Search     |\n";
  cout<<"\t\t\t\t|   11.| SubString  |\n";
  cout<<"\t\t\t\t+------+------------+\n";

  cout<<"\n\tEnter your Choice: ";
  cin>>(*choice);
}

//Input function with no Argument
void MyString::Input(void)
{
  int i=0;
  char ch;
  cout<<"\n\n\tEnter the String: \n";
  do
  {
    ch= getchar();
    A[i]=ch;
    i=i+1;
  }while(ch!='\n');
  A[i-1]='\0';
}
 
//Input function with one Argument
int MyString::Input(char B[40])	
{
  int i=0;
  char ch;
  cout<<"\n\n\tEnter the String: \n";
  do
  {
    ch= getchar();
    B[i]=ch;
    i=i+1;
  }while(ch!='\n');
  B[i-1]='\0';
  return(0);
}

//Display function with no Argument
int MyString::Display(void)	
{
  int i=0;
  while(A[i]!='\0')
  {
    cout<<A[i];
    i=i+1;
  }
  return(0);
}

//Display function with one Argument
int MyString::Display(char B[40])	
{
  int i=0;
  while(B[i]!='\0')
  {
    cout<<B[i];
    i=i+1;
  }
  return(0); 
}

//Length function with two Arguments
int MyString::Length(int *len,int *itt)
{
  (*len)=0;
  (*itt)=0;
  int i=0;
  while(A[i]!='\0')
  {
    i++;
    (*itt)=((*itt)+1);
  }
  (*len)=i;
  return(*len);
}

//Length function with three Arguments
int MyString::Length(int *len, char B[40],int *itt)
{
  (*len)=0;
  (*itt)=0;
  int i=0;
  while(B[i]!='\0')
  {
    i++;
    (*itt)=((*itt)+1);
  }
  (*len)=i;
  return(*len);
}

//Reverse function with three Arguments
int MyString::Reverse(int len,char B[40],int *itt)
{
  int i,j;
  i=(len-1);
  j=0;
  while(i>=0)
  {
    (*itt)=((*itt)+1);
    B[j]=A[i];
    j++;
    i--;
  }
  B[j]='\0';  
} 

//Palindrome function with two Arguments
int MyString::Palindrome(int *len,int *itt)
{
  int i,j;
  i=0;
  j=(*len-1);
  while(i<=j)
  {
    (*itt)=((*itt)+1);
    if(A[i]!=A[j])
      return(1);       
    else
    {
      i++;
      j--;
    }
  }
  return(0);
}

//Concatenate function with three Arguments
void MyString::Concatenate(char B[40],char C[80],int *itt) 
{
  int i,j;
  (*itt)=0;
  i=0;
  while(A[i]!='\0')
  {
    (*itt)=((*itt)+1);
    C[j]=A[i];
    i++;
    j++;
  }
  C[j]=' ';
  j++;
  i=0;
  while(B[i]!='\0')
  {
    (*itt)=((*itt)+1);
    C[j]=B[i];
    i++;
    j++;
  }
  C[j]='\0';
}

//Compare function with three Arguments
int MyString::Compare(char B[40], int *flag,int *itt)
{
  int i,j;
  j=0;
  i=0;
  (*flag)=0;  

  while( ((A[i]!='\0') && (B[j]!='\0')) && (A[i]==B[j]) 	)
  {
    (*itt)=((*itt)+1);
    i++;
    j++;
  }

  (*itt)=((*itt)+1);
  if(A[i]==B[j])
  {
    (*flag)=0; 
  }
  else if(A[i]!=B[j])
  {
    if(A[i]=='\0')
    {
      (*flag)=1;   
    }
    else if(B[j]=='\0')
    {
      (*flag)=-1;   
    }
    else if(A[i]<B[j])
    {
      (*flag)=1;  
    }
    else if(A[i]>B[j])
    {
      (*flag)=-1;    
    }
  }
  return(*flag);
}

//Abbreviate function with two Arguments
void MyString::Abbreviate(char B[40],int *itt)
{
  int i,j;
  (*itt)=0;
  i=0;
  j=0;
  while(A[i]!='\0')
  {
    while(A[i]==' ')
    {
      (*itt)=((*itt)+1);
      i++;
    }

    (*itt)=((*itt)+1);
    if((A[i]>=97) && (A[i]<=122))
    {
      B[j]=A[i]-32;
      j++;
      B[j]='.';
      j++;
    }
    else 
    {
      B[j]=A[i];
      j++;
      B[j]='.';
      j++;
    }
    i++;
    while((A[i]!=' ') && (A[i]!='\0'))
    {
      (*itt)=((*itt)+1);
      i++;
    }
  }
  B[j-1]='\0';
}

//UpperLower function with two Arguments
void MyString::UpperLower(char B[40],int *itt)
{
  int i,j;
  (*itt)=0;
  i=0;
  j=0;
  while(A[i]!='\0')
  {
    while(A[i]==' ')
    {
      (*itt)=((*itt)+1);
      B[j]=A[i];
      i++;
      j++;
    }

    if((A[i]>=97) && (A[i]<=122))
    {
      B[j]=A[i]-32;
    }
    else 
    {
      B[j]=A[i];
    }
    j++;
    i++;
    (*itt)=((*itt)+1);
    while((A[i]!=' ') && (A[i]!='\0'))
    {
      (*itt)=((*itt)+1);
      if((A[i]>=65) && (A[i]<=91))
      {
        B[j]=A[i]+32;
      }
      else 
      {
        B[j]=A[i];
      }
      j++;
      i++;
    }
  }
  B[j]='\0';
}

//Search function with five Arguments
void MyString::Search(char B[40],int *flag,int la,int lb,int *itt1)
{
  int i=0,j,k,l,f;
  l=la-lb+1;
  (*flag)=-1;
  while(i<=l)
  {
    f=0;
    j=0;
    k=i;
    while((B[j]!= '\0') && (f==0))
    {
      (*itt1)=((*itt1)+1);
      if(A[i]==B[j])
      {
        i++;
        j++;
      }
      else
      {
        i=k+1;
        f=1;
      }
    }
    if(B[j]=='\0')
    {
      (*flag)=k;
    }
  }
}

//SubString function with four Arguments
void MyString::SubString(int *I,int *L,char B[40],int *itt)
{
  int i,j,k;
  j=0;
  k=0;
  i=(*I);
  while(k<(*L))
  {
    (*itt)=((*itt)+1);
    if(A[i]=='\0')
    {
      B[j]='\0';
      break; 
    }
    else
    {
      B[j]=A[i];
      j++;
      i++;
      k++;
    }
  }
  B[j]='\0';
}

//Change function with two Arguments
void MyString::Change(char B[40],int *itt)
{
  int j=0;
  (*itt)=0;
  while(B[j]!='\0')
  {
    (*itt)=((*itt)+1);
    if((B[j]>=65) && (B[j]<=90))
      B[j]=B[j]+32;
    j++;
  }
}

//Change function with no Argument
void MyString::Change(void)
{
  int i=0;
  while(A[i]!='\0')
  {
    if((A[i]>=65) && (A[i]<=90))
      A[i]=A[i]+32;
    i++;
  }
}


//=======================	M	A	I	N	=====================

int main(void)
{
 int len=0,pal=0,flag,choice,I,L,la,lb,itt,itt1;
 char B[40],C[80],check;
 MyString t;

 do
 {
   system("clear");
   t.menu(&choice);
  switch(choice)
  {
    case 99:
/*
      exit(0);	//forcefully break
*/
      cout<<"\n\tYou choose 99...\n\n\n\n\tAre you sure you want to close the Program?\n\tIf yes, press 'y', else press 'n'.\n\n";
      getchar();
      check=getchar();
    break;

    case 1:
      getchar();
      t.Input();
      cout<<"\n"; 
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 2:
      getchar();
      cout<<"\n\n\tYou entered: ";
      t.Display();
      cout<<"\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 5:
      getchar();
      t.Length(&len,&itt);
      cout<<"\n\tThe Length of the String is: "<<len<<"\n\n";
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 3:
      getchar();
      cout<<"\n\tThe Main String is: ";
      t.Display();
      cout<<"\n\tThe Reversed String is: ";
      t.Length(&len,&itt);
      t.Reverse(len,B,&itt);
      t.Display(B);
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 4:
      getchar();
      t.Length(&len,&itt);
      pal=t.Palindrome(&len,&itt);
      if(pal==1)
        cout<<"\n\tThe String is not Palindrome."; 
      else
        cout<<"\n\tThe String is Palindrome.";
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 7:
      getchar();
      t.Input(B);
      t.Concatenate(B,C,&itt);
      t.Display(C);
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 6:
      getchar();
      t.Change();
      t.Input(B);
      t.Change(B,&itt);
      t.Compare(B,&flag,&itt);
      if(flag==(-1))
        cout<<"\n\tFirst string is Greater than the Second string.\n";
      else if(flag==1)
        cout<<"\n\tSecond string is Greater than the First string.\n";
      else if(flag==0) 
        cout<<"\n\tBoth Strings are Equal.\n";
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 8:
      getchar();
      t.Abbreviate(B,&itt);
      t.Display(B);
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 9:
      getchar();
      t.UpperLower(B,&itt);
      t.Display(B);
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    case 10:
      getchar();
      t.Input(B);
      la=t.Length(&len,&itt);
      itt1=itt;    
      lb=t.Length(&len,B,&itt1);
      t.Search(B,&flag,la,lb,&itt1);
      cout<<"\n\tMain String: ";
      t.Display();
      if(flag==(-1))
        cout<<"\n\tEntered String is not found...\n";
      else
        cout<<"\n\tEntered string ["<<B<<"] is found at position:"<<flag;
      cout<<"\n\tItteration number: "<<itt1<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;
   
    case 11:
      getchar();
      la=t.Length(&len,&itt);
      cout<<"\n\tEnter the starting Index of the SubString: ";  
      cin>>I;
      if((I>la)||(I<0))
      {
        cout<<"\n\tSubString formation is not Possible...\n";
        cout<<"\n\n\n\t\tPress Enter Key...";
        getchar();
        break;
      }
      cout<<"\n\tEnter the Length of the SubString: ";
      cin>>L;
      getchar();
//      system("clear");
      if((I+L)>la)
      {
        cout<<"\n\tSubString formation is not Possible...\n";
      }
      else 
      {
        t.SubString(&I,&L,B,&itt);
        cout<<"\n\tThe SubString is: ";
        t.Display(B); 
      }
      cout<<"\n\tItteration number: "<<itt<<"\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
    break;

    default:
      cout<<"\n\n\n\tYou choose Wrong option...\n\n\n\n\n";
      cout<<"\n\n\n\t\tPress Enter Key...";
      getchar();
       	
  }
  getchar();
 }while(check!='y');
 return(1);
}
Arup Goswami
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date