arrayas

hey i'm new to c++and am having a lot of trouble with this program my professor gave me. i really am not sure how i should start. this is the problem



A palindrome is a word or phrase that is the same forwards and backwards. For example
the word "radar" is a palindrome. Write a program that reads in a word from the keyboard
and determines if the word is a palindrome. The program does not need to determine if
the characters entered are actually a word. The word will be terminated by the value ".".
The "." is not part of the word and should not be looked at when determining the
palindrome. You should ignore case when comparing letters. The characters should be
read into a character array. The maximum word length is 20. Your program must use a
function called pal that takes a char array and an int size as parameters. The function
will return an int value of 1 if the array contains a palindrome and it will return a value
of 0 if it is not a palindrome. Your program should include a loop that lets the user repeat
the palindrome determination until the user says she or he is done.



Example Output:
Enter a word (. to end the word): radar.
The word "radar" is a palindrome.
Would you like to check another palindrome? (Y or N): Y
Enter a word (. to end the word): library.
How do you know if a word is a palindrome? Once you understand how you know, you can turn that into code.

This is what coding actually is. All the syntax of a language is just learning to speak; coding is the logical expression of problem solving.
Last edited on
well its a palindrome if its spelled the same way backward and forward. so i was thinking to use a for statement to reverse it and and if statement to check if they are equal, but that for some reason does'nt sound right to me.
That is a perfectly reasonable way to go about it. Now try to put it into code.
What about following these steps:

Get the char array size.
check if the array first element->0 is equal to the array's size minus 1(which will be the terminating character '/0).
check if the array element first+1=last-1 and do so untill you reach the element arraysize-1/2(you must include different situations for when the size is even or not because there may or may not be an independent member in the array)

CHECK THIS:

http://www.cplusplus.com/forum/beginner/54885/
Last edited on
That is what's been established...
ok this is what i have. but it puts everything out as a palindrome and im not seeing what im doing wrong.


har input[20];
int palin;
int i=0;
char ans;


do
{

cout<<"Enter a word (. to end the word): ";
cin>>input;
if(input[i]!='.')
i++;

palin=pal(input,i);
if(palin==1)
cout<<"The word"<<input<<" is a Palindrome\n\n";
else
cout<<"The word" << input<<" is NOT a Palindrome\n\n";
cout<<"Would you like to check another palindrome?(y/n) ";
cin>>ans;

}
while(ans=='Y'||ans=='y');
cout << endl;
cout <<"End Program";
return 0;
}

int pal( char input[],int n)
{
int i=0;
int j=n-1;
for((input)[i]=(input[j]);i<=j;i++)
{
i++;
j--;

cout<< input[4]<<endl<<input[j]<<endl;
}

if(i>j)
return 1;
return 0;
}
I tried out that code and it didn't work :/ A simpler way to do this would just be to copy the first string into a second string in reverse, and see if the two are equal. If they aren't, the word isn't a palindrome :/
Last edited on
I'm a begginer too. Even though , this may help you:

Remember you can check each space of a string as if it were an array by using:

nameofstring[position];

That should make it a lot easier.

BTW, there are easier ways to check a palindrome without using an external function. Here's an example:


#include <iostream>

using namespace std ;

int main () {
string s;
cin>>s;
int sz = s.size();
char x [sz];
int i , j;
for (j=0;j<sz;j++) {
x[j]=s[sz-j-1];// the -1 is because the array starts on 0....
}
bool b=true;
for (i=0;i<sz;i++) {
if (x[i]!=s[i]) {b=false;}
}
if (b==true) {cout<<"palindrome."<<endl;}
else {cout<<"not a palindrome"<<endl;}
system("pause");
}
Last edited on
Why is that way easier? It's a lot nicer to have it in a function so you can easily reuse the code both in the current project and others. He seems to understand array indexing, it's just some of his code was slightly screwed :/
My bad, as I wrote , I'm a begginer too. It can be done as I wrote but instead of printing the result , returning 0/1.
Last edited on
ok, thanks , but how exactly would i do that. @ascii.
Last edited on
Think about it... how would you store a reversed string in another string... and how would you compare the two?
Topic archived. No new replies allowed.