fork(1) download
  1. #include <iostream> // cout
  2. #include <stdio.h> // printf
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. bool is_palidrome(string inputString) {
  8. int num_to_parse = inputString.size();
  9.  
  10. /* immediately return on single letter string as all single letter strings are palindromes -- saves CPU time as programme does not need to enter the loop */
  11. if(num_to_parse == 1) {
  12. return true;
  13. }
  14.  
  15. for(int i=0; i<num_to_parse; i++) {
  16. int point_end = num_to_parse - i - 1;
  17. if(inputString[i] == inputString[point_end]) {
  18. continue;
  19. } else if (i == num_to_parse) {
  20. return true;
  21. } else {
  22. return false;
  23. }
  24. }}
  25.  
  26.  
  27. int main() {
  28. // Change the value of inputString to test different palindromes
  29. string inputString = "caabaac";
  30. printf("%s", is_palidrome(inputString)?"This is a palindrome.":"Not a palindrome."); // ternary: test condition ? return this if true : return this if false
  31. return 0;
  32. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Not a palindrome.