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>(*choice);}//Input function with no Argumentvoid MyString::Input(void){  int i=0;  char ch;  cout
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