Question:
Write a c++ program to check whether a string is a palindrome or not?
2013-11-28 02:21:21 UTC
I am a beginner and i am in class 11. So, please help me out of this and please tell the simple and understanding program. Please.
Thanks
Four answers:
?
2013-11-28 02:28:05 UTC
include

#include



int main()

{

char a[100], b[100];



printf("Enter the string to check if it is a palindrome\n");

gets(a);



strcpy(b,a);

strrev(b);



if( strcmp(a,b) == 0 )

printf("Entered string is a palindrome.\n");

else

printf("Entered string is not a palindrome.\n");



return 0;

}
noob
2013-11-28 08:54:20 UTC
Well there are several ways of doing it. The easiest is like this:



Take a string as an input.

Check if every first and last characters match. For example: to check the word "madam", your steps should be,

MadaM

mAdAm

maDam



And as all the steps match, it is a palindrome.



Here is a program written in C:



#include

#include



int main()

{



int i, j, len, is_palindrome;

char str[1000];



scanf("%s", str); // Taking the string as input



len = strlen(str); // Finding the length of the string

is_palindrome = 1; // Assuming the string is a palindrome



for (i=0,j=len-1; i < j; i++,j--) // i runs from the start of the string and j from the end of the string

{

if (str[i] != str[j])

{

is_palindrome = 0; // Whenever there is a mismatch, the is_palindrome variable is set to 0 and the loop is broken

break; // This statement breaks the loop before it actually finishes the normal way

}

}



if (is_palindrome == 1) // If there is no mismatch during the loop, then this variable stays as 1

{

printf("It is a palindrome.\n");

}

else

{

printf("It is not a palindrome.\n");

}



return(0);

}
2013-11-28 07:51:55 UTC
Hello friend,



You can download program from these links...



http://projectsgeek.com/2011/04/operator-overloading-on-string-functions-c-language.html



Also try this Link....



http://projectsgeek.com/2011/09/string-operations-such-as-copy-length-reversing-palindrome-concatenation.html
Ashish
2013-11-28 18:23:32 UTC
the best and fastest way to implement this -->



#include

#include

using namespace std;



bool pallin(char[] str) {

for(int i = 0, j = strlen(str); i < j; i++; j--) {

if(str[i] != str[j]) return false;

}

return true;

}

int main() {

char str[100]; /*string */

gets(str);

if(pallin(str)) cout<<"pallindrome";

else cout<<"not pallindrome";

}



I hope so the logic is clear. A simple for loop did all the things. You dont need to reverse the string.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...