devxlogo

A Simple String Class for Beginners

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#include#includeusing 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<<"







					Software initialising...
					Preparing for First time use...














					Press Enter key...

";    getchar();    system("clear");    cout<<"







					This is the Demonstration of MyString Class...














					Press Enter key...

";    getchar();    system("clear");    Input();  }//Destructor ~MyString(void)		  {    system("clear");    cout<<"









					System is shutting down...

					Good Bye...











";  }};//menu functionvoid MyString::menu(int *choice){  cout<<"
";  cout<<"				   +-------------+
";  cout<<"				   |  MyString   |
";  cout<<"				   +-------------+


";  cout<<"				+------+------------+
";  cout<<"				|OPTION| DETAILS    |
";  cout<<"				+------+------------+
";  cout<<"				|   99.| Exit       |
";  cout<<"				|    1.| Input      |
";  cout<<"				|    2.| Display    |
";  cout<<"				|    3.| Reverse    |
";  cout<<"				|    4.| Palindrome |
";  cout<<"				|    5.| Length     |
";  cout<<"				|    6.| Compare    |
";  cout<<"				|    7.| Concatenate|
";  cout<<"				|    8.| Abbreviate |
";  cout<<"				|    9.| UpperLower |
";  cout<<"				|   10.| Search     |
";  cout<<"				|   11.| SubString  |
";  cout<<"				+------+------------+
";  cout<<"
	Enter your Choice: ";  cin>>(*choice);}//Input function with no Argumentvoid MyString::Input(void){  int i=0;  char ch;  cout<<"

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

	Enter the String: 
";  do  {    ch= getchar();    B[i]=ch;    i=i+1;  }while(ch!='
');  B[i-1]='';  return(0);}//Display function with no Argumentint MyString::Display(void)	{  int i=0;  while(A[i]!='')  {    cout<=0)  {    (*itt)=((*itt)+1);    B[j]=A[i];    j++;    i--;  }  B[j]='';  } //Palindrome function with two Argumentsint 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 Argumentsvoid MyString::Concatenate(char B[40],char C[80],int *itt) {  int i,j;  (*itt)=0;  i=0;  while(A[i]!='')  {    (*itt)=((*itt)+1);    C[j]=A[i];    i++;    j++;  }  C[j]=' ';  j++;  i=0;  while(B[i]!='')  {    (*itt)=((*itt)+1);    C[j]=B[i];    i++;    j++;  }  C[j]='';}//Compare function with three Argumentsint MyString::Compare(char B[40], int *flag,int *itt){  int i,j;  j=0;  i=0;  (*flag)=0;    while( ((A[i]!='') && (B[j]!='')) && (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]=='')    {      (*flag)=1;       }    else if(B[j]=='')    {      (*flag)=-1;       }    else if(A[i]B[j])    {      (*flag)=-1;        }  }  return(*flag);}//Abbreviate function with two Argumentsvoid MyString::Abbreviate(char B[40],int *itt){  int i,j;  (*itt)=0;  i=0;  j=0;  while(A[i]!='')  {    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]!=''))    {      (*itt)=((*itt)+1);      i++;    }  }  B[j-1]='';}//UpperLower function with two Argumentsvoid MyString::UpperLower(char B[40],int *itt){  int i,j;  (*itt)=0;  i=0;  j=0;  while(A[i]!='')  {    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]!=''))    {      (*itt)=((*itt)+1);      if((A[i]>=65) && (A[i]<=91))      {        B[j]=A[i]+32;      }      else       {        B[j]=A[i];      }      j++;      i++;    }  }  B[j]='';}//Search function with five Argumentsvoid 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]!= '') && (f==0))    {      (*itt1)=((*itt1)+1);      if(A[i]==B[j])      {        i++;        j++;      }      else      {        i=k+1;        f=1;      }    }    if(B[j]=='')    {      (*flag)=k;    }  }}//SubString function with four Argumentsvoid 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]=='')    {      B[j]='';      break;     }    else    {      B[j]=A[i];      j++;      i++;      k++;    }  }  B[j]='';}//Change function with two Argumentsvoid MyString::Change(char B[40],int *itt){  int j=0;  (*itt)=0;  while(B[j]!='')  {    (*itt)=((*itt)+1);    if((B[j]>=65) && (B[j]<=90))      B[j]=B[j]+32;    j++;  }}//Change function with no Argumentvoid MyString::Change(void){  int i=0;  while(A[i]!='')  {    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<<"
	You choose 99...



	Are you sure you want to close the Program?
	If yes, press 'y', else press 'n'.

";      getchar();      check=getchar();    break;    case 1:      getchar();      t.Input();      cout<<"
";       cout<<"


		Press Enter Key...";    break;    case 2:      getchar();      cout<<"

	You entered: ";      t.Display();      cout<<"
";      cout<<"


		Press Enter Key...";    break;    case 5:      getchar();      t.Length(&len,&itt);      cout<<"
	The Length of the String is: "<>I;      if((I>la)||(I<0))      {        cout<<"
	SubString formation is not Possible...
";        cout<<"


		Press Enter Key...";        getchar();        break;      }      cout<<"
	Enter the Length of the SubString: ";      cin>>L;      getchar();//      system("clear");      if((I+L)>la)      {        cout<<"
	SubString formation is not Possible...
";      }      else       {        t.SubString(&I,&L,B,&itt);        cout<<"
	The SubString is: ";        t.Display(B);       }      cout<<"
	Itteration number: "<
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist