fork(5) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // checks whether the given string is a palindrome
  7.  
  8. int is_palindrome(string s)
  9. {
  10. int i;
  11. int half_size = s.size()/2;
  12.  
  13. // check all characters (except for middle one if size is odd)
  14. // on whether it is equal to the opposite character across the
  15. // middle of the string
  16.  
  17. for (i=0; i<half_size; i++)
  18. if (s[i] != s[s.size()-1-i])
  19. return 0;
  20.  
  21. // if we got this far, we already know that it is a palindrome,
  22. // because all characters matched as they should
  23.  
  24. return 1;
  25. }
  26.  
  27. // main function
  28.  
  29. int main()
  30. {
  31. string s;
  32.  
  33. // read string s from input
  34.  
  35. cin >> s;
  36.  
  37. // check whether the string is a palindrome, and print out
  38. // the result
  39.  
  40. if ( is_palindrome(s) )
  41. cout << s << " is a palindrome." << endl;
  42. else
  43. cout << s << " is not a palindrome." << endl;
  44.  
  45. // return 0 from main
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 2860KB
stdin
civic
stdout
civic is a palindrome.