How To Check Whether The Given String Is A Palindrome Using Pointers In C++?

1

1 Answers

John Nawrocki Profile
John Nawrocki answered
// Test to see if a string is a palindrome
#include \\\"stdafx.h\\\"
#include
#include
#include
void test(char message[]);
int Palindrome(char message[]);
void main( )
{
char message1[] = "able was I ere I saw elba";
char message2[] = "this is a toyota";
char message3[] = "a toyota";
 
test(message1);\\
test(message2);\\
test(message3);\\
return;
}
void test(char message[])
{
printf("%s ",message);
if (Palindrome(message)) //Send pointer to message
 printf("is a palindromen");
else
 printf("is not a palindromen");
}
int Palindrome(char message[])
{
 // find end of message
int start, end, ret;
ret = 1;  // set return code to true
for (end = 0; message[end]; ++end); // find end of message
end -= 1; // backup to last character of message
for(start = 0;start != end; ++start,--end) // loop thru until we meet 
{
 if (message[start] == ' ') ++start; // ignore blanks 
 if (message[end] == ' ') --end; //
 if (message[start] != message[end]) // compare front 2 back
 {
  ret = 0; // if not equal change return code to false 
  break;   // and get out  
  }
}
return(ret); // return true or false
}
thanked the writer.
John Nawrocki
John Nawrocki commented
Code does not display well in html. If you would like to me to email a formatted compilable version, just let me know.

Answer Question

Anonymous