×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to check if the string is palindrome using class

Submitted by Shubh Pachori, on September 06, 2022

Problem statement

Given a string, we have to check if the string is palindrome using the class and object approach.

Example:

Input:
Enter String: lol

Output:
String is a palindrome!

C++ code to check if the string is palindrome using the class and object approach

#include <iostream>
using namespace std;

// create a class
class String {
  // private data member
 private:
  char str[30];

  // public member functions
 public:
  // getString() function to store string
  void getString() {
    cout << "Enter String: ";
    cin.getline(str, 30);
  }

  // palindrome() function to check if the
  // string is palindrome ot not
  void palindrome() {
    // initialising variables to 
    // perform operations
    char temp_str[30];
    int index_1, index_2, end, check;

    // for loop to find the length of the string
    for (end = 0; str[end]; end++)
      ;

    // for loop to reverse the string and copy 
    // it in another string
    for (index_1 = end - 1, index_2 = 0; index_1 >= 0; index_1--, index_2++) {
      temp_str[index_2] = str[index_1];
    }

    // transfering null
    temp_str[index_2] = 0;

    // for loop to check both the strings
    // are palindrome or not
    for (index_1 = 0; str[index_1]; index_1++) {
      // if condition to check both the strings
      if (str[index_1] == temp_str[index_1]) {
        check++;
      } else {
        check = 0;
        break;
      }
    }

    // if condition to print if the string
    // is palindrome or not
    if (check != 0) {
      cout << "String is a palindrome!" << endl;
    } else {
      cout << "String is not a palindrome!" << endl;
    }
  }
};

int main() {
  // create an object
  String S;

  // calling getString() function 
  // to insert string
  S.getString();

  // calling palindrome() function 
  // to check the string
  S.palindrome();

  return 0;
}

Output

RUN 1:
Enter String: LoL
String is a palindrome!

RUN 2:
Enter String: HellolleH
String is a palindrome!

Explanation

In the above code, we have created a class String, one char type array data members str[30] to store the string, and public member functions getString() and palindrome() to store the string and to check if the string is palindrome or not.

In the main() function, we are creating an object S of class String, reading the inputted string by the user using the getString() function, and finally calling the palindrome() member function to check if the string is palindrome or not. The palindrome() function contains the logic to check if the string is palindrome or not and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.