diff options
| author | Fuwn <[email protected]> | 2024-04-07 23:18:32 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-04-07 23:18:32 -0700 |
| commit | c1b6ffe70bd281c6c230fd63fabcaac2aff47514 (patch) | |
| tree | e8af3b1782a7cd0754590ed618fddc1bdb9b7385 /chapter8/pal.cxx | |
| download | dscode-main.tar.xz dscode-main.zip | |
Diffstat (limited to 'chapter8/pal.cxx')
| -rw-r--r-- | chapter8/pal.cxx | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/chapter8/pal.cxx b/chapter8/pal.cxx new file mode 100644 index 0000000..11153e8 --- /dev/null +++ b/chapter8/pal.cxx @@ -0,0 +1,44 @@ +// FILE: pal.cxx
+// Program to test whether an input line is a palindrome. Spaces,
+// punctuation, and the difference between upper- and lowercase are ignored.
+
+#include <cassert> // Provides assert
+#include <cctype> // Provides isalpha, toupper
+#include <cstdlib> // Provides EXIT_SUCCESS
+#include <iostream> // Provides cout, cin, peek
+#include <queue> // Provides the queue template class
+#include <stack> // Provides the stack template class
+using namespace std;
+
+int main( )
+{
+ queue<char> q;
+ stack<char> s;
+ char letter;
+ queue<char>::size_type mismatches = 0; // Mismatches between queue and stack
+ cout << "Enter a line and I will see if it's a palindrome:" << endl;
+
+ while (cin.peek( ) != '\n')
+ {
+ cin >> letter;
+ if (isalpha(letter))
+ {
+ q.push(toupper(letter));
+ s.push(toupper(letter));
+ }
+ }
+
+ while ((!q.empty( )) && (!s.empty( )))
+ {
+ if (q.front( ) != s.top( ))
+ ++mismatches;
+ q.pop( );
+ s.pop( );
+ }
+
+ if (mismatches == 0)
+ cout << "That is a palindrome." << endl;
+ else
+ cout << "That is not a palindrome." << endl;
+ return EXIT_SUCCESS;
+}
|