From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter8/pal.cxx | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 chapter8/pal.cxx (limited to 'chapter8/pal.cxx') 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 // Provides assert +#include // Provides isalpha, toupper +#include // Provides EXIT_SUCCESS +#include // Provides cout, cin, peek +#include // Provides the queue template class +#include // Provides the stack template class +using namespace std; + +int main( ) +{ + queue q; + stack s; + char letter; + queue::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; +} -- cgit v1.2.3