#include <iostream>
#include "isPalindrome.h"
usingnamespace std;
int main()
{
isPalindrome word;
std::string CheckWord;
cout << "To verify a palindrome word, Input your word :" ;
cin >> CheckWord;
cout << word.Word(CheckWord);
return 0;
/*------------------------------------------------------------------------------------
======================================================================================
======================================================================================
isPalindrome A palindrome is a word that reads the same forwards and backwards; For
example “noon” or “racecar”. Write a function isPalindrome which takes an array of
characters and the length of that array; returning true if the characters form a valid
palindrome and false otherwise.
Your main program will use this function to test the following possible palindromes:
“racecar”, “step on no pets”, “devil lived”, “elephant”, “nothing” and for each one
output it and then say whether it is a palindrome or not.
============================================================================================
============================================================================================
------------------------------------------------------------------------------------------*/
}
#include "isPalindrome.h"
isPalindrome::isPalindrome()
{
//ctor
}
/*Edited********************************************************
Should this work for a start?
**************************************************************/
std::string isPalindrome::Word()
{
std::string word;
int countLetters = 0;
char letter;
while (letter!= '0'){
letter = word[countLetters];
countLetters++;
}
}
/*Edited*******************************************************/
isPalindrome::~isPalindrome()
{
//dtor
}
You'll want to create an array for characters to store the palindrome and copy it to another array.
char word[10];
char isPalindrone[10];
cin >> word;
int count = 9;
for ( int x = 0; x < 10; x++ )
{
isPalindrone[count] = word[x]
count--; }
Now, to check if the reverse is the same as the original word.
for ( int x = 0; x < 10; x++ )
{ isPalindrone[x] == word[x]; }
From there you'll create a bool function to return TRUE if it is indeed a perfect match, or false if it is not. Be sure to initialize both arrays to a unused character such as '@' or something.
Don't have usingnamespace std; did I mention this in another post?
You have a constructor isPalindrome(std::string) , make use of it - get your input first then create the object with it.
Should you name the class Palindrome and have a function isPalindrome that returns a bool type?
You have the compiler error because your function definition & declaration don't match. But that might be negated by my previous sentence.
If you have compiler errors, then always post them in full and make sure the line numbers match so we can see what is going on.
To check that something is a palindrome, compare the first & last char - if they are equal then compare second & second to last, and so on until finished. Obviously if a comparison is false then it's not a palindrome. This is better than reversing a string, because that effort is wasted when it can be detected it is not a palindrome very early on.
How do I ask the program how many characters are in the string array?
should I have 2 functions taking the word simultaneously?
And yes You did tell me about (std::) but I wrote most of this code yesterday. I just posted it when I finished the code from the other post. I also have another thread going that needs attention. not as much as this one. and along the same line.
How do I ask the program how many characters are in the string array?
The std::string class has a bunch of member functions - look them up in the reference section at the top left of this page.
should I have 2 functions taking the word simultaneously?
Just use the constructor you have which takes a string as an argument, which should use that string to initialise a member variable via the initialisation list. Then have the IsPlaindrome function.