C++

Check for palindrome C++

We usually use strings in our programs by applying different operations to them. Sometimes such a situation occurs when there is a need to get the resultant value in the reverse order. The same letters in the string with the reverse order are known as a ‘palindrome’. In C++, there are several ways to check if the string or numeric values are palindromes. In this guide, we have used three different approaches to check the status of the string.

Sample example:

Input string: GODOG

Reverse: GODOG

Output: It is a palindrome because the reverse is equivalent to the input string.

Implementation

Example 1

In this example, we will take a string as an input and will check if the reverse of that string is the same or not. We will go for a simple approach in which we will use a built-in function of the C++ ‘reverse’ function directly to create the reverse of the string. But at the start, we need to include a library that will make the program user capable of inheriting some modules and features included in the header file.

#include <bits/stdc++.h>

After introducing the library, a function named ‘ispalindrome’ is created to check if the given string is a palindrome. It will take the string as a parameter because the function reverse will be applied to this string. A string-type variable will store the passed argument, and later on, this variable will be used. We will call that built-in function, which will contain two parameters. These two parameters themselves are built-in features in C++. The begin() function deals with returning the string’s first character. At the same time, the end() function is the one that returns the iterator that points towards the end character of the string. Both functions will be accessed through the string variable. These functions are the features of string functions.

Reverse ( P.begin(), P.end());

As a result, the starting and ending characters are swapped. Then we use an if-else statement to check the reversed string if it is similar to the entered string or not. If it matches, the message ‘yes’ is displayed; otherwise, it is ‘No’.

Outside the function body, the main program is declared here. A string is introduced here. The string ‘ABCDCBA’ contains 7 letters; the first 3 and the last 3 letters are the same. So on reversing them, the resultant value will be the same.

Ispalindrome(s) ;

Save the source code in the file with any name extension of ‘c’. And then compile it by using a compiler. The compiler used for the compilation of the C++ programming language is G++.

$ g++ -o sample sample.c
$ ./sample

On the file’s execution, you will see that ‘yes’ is the resultant value.

Example 2

In this example, we have involved the user. We will ask the user to enter his desired value, so the system can check and display the result. The library used for the program is ‘iostream’; because of this library, we can perform cin and cout features; in other words, the user can interact with the program by using this header file.

#include <iostream>

In this function, we do not have to use the built-in reverse function, ‘reverse’ in the program. Another approach is used here that uses the involvement of the do-while loop. Here the whole logic is applied in the main program instead of using a separate function. We declare four integer type variables. One will store the value that is entered by the user. So the first step is asking the user to enter the value. The variable that stores the value.

After that, the do-while loop will be used in which the number that the user enters will be subjected to have a modulus of 10. Then the value of reverse that was initially initialized as zero is multiplied by 10. This will be zero again, but by following the rules, we have to do it so that value is added to the value stored in the digit variable. And at the end, the number is divided by 10. The condition entered in the while loop is that the loop will continue iteration until the number variable has to value.

Display the reverse of the string. And then use an if-else statement to check the palindrome nature of the given string. If the reversed value is equal to the entered one, then the message is displayed that the number is a palindrome.

On execution, you will see that when the user is asked to enter the value, he has entered ‘1221’. This contains 4 letters, and the starting numbers are equivalent to the ending ones. So the reverse will be the same, and hence it is a palindrome.

If we enter ‘56756’. This time the values in the starting and ends are not equal, so the reverse will not be the same; hence this number will not be a palindrome.

Example 3

In this example, we have used three libraries to support our source code. Two of them are described earlier. The third one is that it takes the string value so that we will use the string function separately.

#include <string.h>

In the main program, the string variable will be declared as a string in a character array, so the character array of 100 sizes is initialized here.

Char s1[100].

This example also includes the involvement of the user. So the user will enter the value of his desire. Again, we will not use the system to define the method, the built-in function, ‘reverse’. A while loop is used, which will iterate until the terminating character is inside the character array until the end. Inside the while loop, the string character is entered into the variable. An integer type variable will contain the value obtained by subtracting one from the value at the specified index.

N1 = I – 1;

The character will take the addition of value in a specified index and 1. The ‘n’ is used to display the size of the string.

The computing logic will also contain a while loop, as it is the string, so to access each character; we need a loop.

S2[i] = S1 [n – I -1];
i++ ;

This will calculate the reverse value, and it will be displayed then. Similarly, if the reverse is the same, it will be displayed as a palindrome. Otherwise, it is not. Now we will execute the file to see the results. The user will be asked to enter the value, the total numbers will be calculated, and the result is displayed, which means the string is a palindrome.

A numeric value is entered; its reverse is not equal, so it is not a palindrome.

Conclusion

The article ‘check for the palindrome C++’ is about the checking process of the input statement if it is a palindrome or not. This is done by having the reverse and comparing it with the input value. We have undergone three different methodologies. We are hopeful this article will be favorable for the users.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.