This is a C++ program that checks whether a given string is a palindrome or not. A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward. For example, "racecar" is a palindrome. The program works as follows:
One potential issue with this program is that the reversed string b is not initialized before it is used to store the reversed characters of the original string a. This can lead to undefined behavior, as the program is accessing memory that has not been allocated for the string b. To fix this, the string b should be initialized to the same length as a before the loop that reverses the string is executed.
#include<iostream> using namespace std; int main() { int i,n=0,s=0; string a,b; cout<<"\nEnter the string:"; cin>>a; for(i=0;a[i]!='\0';i++) { n++; } for(i=0;i<n;i++) { b[i]=a[n-i-1]; } for(i=0;i<n;i++){ if(b[i]==a[i]){ s++; } } if(s==n) { cout<<"\nThis is a palindrome:"<<a; } else { cout<<"\nThis is not a palindrome:"<<a; } return 0; }To download raw file Click Here
Enter the string:madam This is a palindrome:madam
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions