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: "<
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

positive contribution tech

Technology’s Positive Contributions to Society

Technology has and continues to improve our lives. From the business world to the world of medicine, and our day-to-day lives, you can’t go a day without interacting with at least one form of technology. While some fear technology may be going too far, there are many ways in which

How to Choose From The Best Big Data Platforms in 2023

How to Choose From The Best Big Data Platforms in 2023

As big data continues to become increasingly popular in the business world, companies are always looking for better ways to process and analyze complex data. The process critically depends on the platform that manages and analyzes the data. In this article, we will provide a guide to help you choose

Why transparent code is a good idea

Why Transparent Code is a Good Idea

Code is used to make up the apps and software we use every day. From our favorite social media platforms to our online banking services, code is the framework used to build these tools that help make our lives easier. Code is complex. Software today requires large teams of programmers

The Role of WordPress Hosting in Website Speed and Performance

The Role of WordPress Hosting in Website Performance

The term “WordPress hosting” refers to a specific type of web hosting service that offers hardware and software configurations tailored to the needs of WP sites. It’s important to remember that a WP hosting package is not required to host WordPress webpages. WP web pages are also compatible with standard

Data Privacy vs. Data Security: What you Should Know

Data Privacy vs. Data Security: What you Should Know

Data privacy and data security are often used interchangeably, but they are two completely different things. It’s important to understand the difference for anyone who handles sensitive information, such as personal data or financial records. In this article, we’ll take a closer look at data privacy vs. data security. We’ll