1.
A palindrome is a word, phrase, or sentence that reads the same forwards and backwards.
Write a function
isPalindrome that takes a string input as a parameter and returns a boolean that is true if the input is a palindrome and false otherwise. Run and test your code!
Solution.
Below is one way to implement the program. We use the
isalpha function to ignore the non alphabetical characters. Then we continuously check to see if the letters in the front are equal to the ones in the back until we reach the middle of the string.
#include <iostream>
#include <cctype>
using namespace std;
bool isPalindrome(string input) {
size_t front = 0;
size_t back = input.length() - 1;
while (front < back) {
while (!isalpha(input[front])) {
front++;
}
while (!isalpha(input[back])) {
back--;
}
if (input[front] != input[back]) {
return false;
}
front++;
back--;
}
return true;
}
