You are here:Home » C++ » Palindromes in C++

Palindromes in C++

Design this program called palindrome.cpp to accept the input of one string and output whether it is a palindrome or not.

Use your reverser and alphaonly code inside of a palindrome function which will return true or false.Rough outline:
#include <string>
#include <iostream.h>

string reverser(string s) {
  string backwards=""; 
  ...
  return backwards;
} 

string alphaonly(string s) {
  string alpha=""; 
  ...
  return alpha;
} 

bool palindrome(string s) { 
  if ( alphaonly(s) == reverser(alphaonly(s)) ) return true;
  else return false;
} 

void main(void) {
  //get the string from the user
  ...
  getline(cin,s);
  if (palindrome(s)) cout << "yep";
  else cout << "nope";
}

For an extra challenge, find out what an anagram is and implement that on a given word or sentence. Hint: You will need to learn how to use rand and srand. What do "Disney World" and "Old dry swine" have in common? How about "The Searchers" and "Three chasers"?

0 comments:

Post a Comment