Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Strings to test a Palindrome

dv8anlydv8anly Member Posts: 1
Hi everyone my name is dva8nly, and I am learining C++ at home on my own and I am working with strings to test a Palindrome, and I am having problems is there anyone out there who can tell me what I am doing wrong! Here is my code:

#include
#include
#include
using namespace std;


bool isPalindrome(string word)
{


for (isPalindrome = 0; level <= len-1; c++)
int d = len(string-1)

if(word [c] == word [d] )
bool word = true;
else
cout << "false
";
//Your code here!
}

int main()
{
string userstr;
cout << "Enter a word or phrase: ";
getline (cin, userstr);

if (isPalindrome(userstr))
cout << "It is a palindrome";
else
cout << "It is not a palindrome";

system("PAUSE");
return 0;
}



Comments

  • DonotaloDonotalo Member Posts: 715
    Try to compile the code and you'll get the errors from the compiler. To get you started, I'm pointing to the first error I found:

    [code]: bool isPalindrome(string word)
    : {
    : for ([color=Red]isPalindrome = 0[/color]; level <= len-1; c++)[/code]

    isPalindrome is a function. You cannot assign 0 as its value.
    --
    ~Donotalo()
  • Malcolm_McLeanMalcolm_McLean Member Posts: 254 ✭✭
    : Hi everyone my name is dva8nly, and I am learining C++ at home on my
    : own and I am working with strings to test a Palindrome, and I am
    : having problems is there anyone out there who can tell me what I am
    : doing wrong! Here is my code:
    :
    : #include
    : #include
    : #include
    : using namespace std;
    :
    :
    : bool isPalindrome(string word)
    : {
    :
    :
    : for (isPalindrome = 0; level <= len-1; c++)
    : int d = len(string-1)
    :
    : if(word [c] == word [d] )
    : bool word = true;
    : else
    : cout << "false
    ";
    : //Your code here!
    : }
    :
    : int main()
    : {
    : string userstr;
    : cout << "Enter a word or phrase: ";
    : getline (cin, userstr);
    :
    : if (isPalindrome(userstr))
    : cout << "It is a palindrome";
    : else
    : cout << "It is not a palindrome";
    :
    : system("PAUSE");
    : return 0;
    : }
    :
    :
    :
    :


    You need to write the isPalindrome function. So you look at the two ends of the word and see if they are the same letter. If they are not, it is not a palindrome, so return false. If they are the same letter, if the positions are adjacent (two letter word) or identical (one letter word) it is a palindrome and you can return true. (You could argue about whether one letter words like I and a are really palindromes, fix that last). Now if you haven't proved it wither way, the word must be three letters or over with the ends matching. So add one to your start position and subtract one from your end position, and repeat the test. Repeat until you get to the centre of the word.

Sign In or Register to comment.