how to write a program wether an input word is a palindrome ex. radar.

Recommended Answers

All 8 Replies

Nice problem. What have you got so far?

#include<iostream.h>
#include<conio.h>
void main()
{
int n,r=0,temp;
clrscr();
cout<<"Enter a number:";
cin>>n;
temp=n;
while(temp>0)
{
r=r*10;
r=r+temp%n;
temp=temp/10;
}
if(n==r)
{
cout<<"\nNumber is palindrome.";
}
else
{
cout<<"\nNumber is not palindrome.";
}
getch();
}

Output:
Enter a number:343
Number is palindrome.
commented: No spoon Feeding please +0
for checking a word

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char x[100];
char y[100];
int temp1,temp2=0;
cout << "Enter the word ";
cin >> x;
temp1 = strlen(x)-1;
for(int j= 0;j<strlen(x);j++)
{
y[j] = x[temp1 - j];
}
y[j] = '\0';
x[j] = '\0';
for(int k =0; k<strlen(x); k++)
{
if (x[k]==y[k]){
temp2 = temp2 + 1;
}
}
if (strlen(x) == temp2)
{cout<<"Palindrome";
}
else
{cout<<"not a palindrome";
}
getch();
}

Something I made for a similar question in "C". Checks lines of text as opposed to words.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>

bool IsPalindrome (const char *string, const int size)
{
    assert (size >= 0);

    // A word consisting of one letter or less is palindrome.
    if (size <= 1)
    {
        return true;
    }
    // non-letters may be skipped.
    else if (!isalpha(string[0]))
    {
        return IsPalindrome(string + 1, size - 1);
    }
    // the same goes for the end
    else if (!isalpha(string[size - 1]))
    {
        return IsPalindrome(string, size - 1);
    }
    // The first and final character are both letters. These must be equal.
    else
    {
        return (tolower(string[0]) == tolower(string[size - 1])) && IsPalindrome(string + 1, size - 2);
    }
}

int main(void)
{
    const char* tests[] = {
        "Bush saw Sununu swash sub.",
        "Do, O God, no evil deed! Live on! Do good!",
        "This is a random text",
        "Flee to me, remote elf.",
        "No, I told Ed \"lotion.\"",
        "Never gonna give you up, never gonna let you down, Never gonna run around and desert you."
    };

    const int testsCount = sizeof(tests) / sizeof(char*);
    int i = 0;

    for (i = 0; i < testsCount; i++)
    {
        if (IsPalindrome(tests[i], strlen(tests[i])))
        {
            printf("\"%s\" is a palindrome!\n", tests[i]);
        }
    }

    return 0;
}
commented: Again! No spoon feeding on DANIWEB +0

And why are you guys giving the OP solutions when he's showed no attempt himself.

commented: Typical "why you give answers???" for trivial problems attitude. +0

Well you could find this easily by comparing the current word with its inverse:

int main(){
    string word="radar";
    if (word==string(word.rbegin(), word.rend())) cout<<"Palindrome.\n";
    else cout<<"Not palindrome.\n";
    return 0;
}

If it's the same than it's a palindrome, otherwise is not.

Gonbe: White knighting.

Well! There was no need to down vote(With negative reps).... OP's Question was not according to rules, and you giving codes (Total Spoon feed) is not in accordance with the daniweb spirit......
Still can't care much...... Leaving the thread......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.