fork download
  1. //=========================================================
  2. // File: HW_10d
  3. // Programmer: Elaine Torrez
  4. // Class: CMPR 121
  5. //=========================================================
  6. // Description:
  7. // This program determines whether a word is
  8. // a palindrome using list iterators.
  9. //=========================================================
  10.  
  11. #include <iostream>
  12. #include <list>
  13. #include <string>
  14.  
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19. list<char> characterList;
  20.  
  21. list<char>::iterator forwardIterator;
  22. list<char>::reverse_iterator reverseIterator;
  23.  
  24. string userWord;
  25.  
  26. bool isPalindrome;
  27.  
  28. int index;
  29.  
  30. cout << "Enter a word: ";
  31. cin >> userWord;
  32.  
  33. for (index = 0; index < userWord.length(); index++)
  34. {
  35. characterList.push_back(userWord[index]);
  36. }
  37.  
  38. isPalindrome = true;
  39.  
  40. forwardIterator = characterList.begin();
  41. reverseIterator = characterList.rbegin();
  42.  
  43. while (forwardIterator != characterList.end())
  44. {
  45. if (*forwardIterator != *reverseIterator)
  46. {
  47. isPalindrome = false;
  48. break;
  49. }
  50.  
  51. forwardIterator++;
  52. reverseIterator++;
  53. }
  54.  
  55. cout << endl;
  56.  
  57. if (isPalindrome == true)
  58. {
  59. cout << userWord
  60. << " is a palindrome."
  61. << endl;
  62. }
  63. else
  64. {
  65. cout << userWord
  66. << " is NOT a palindrome."
  67. << endl;
  68. }
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0.01s 5276KB
stdin
racecar
stdout
Enter a word: 
racecar is a palindrome.