LOL, your professor doesn’t know how to write code. That main is a mess on wheels.
1 •
Don’t loop on EOF.
It doesn’t work like you think it does.
2 • Use pipes, my man. No need to open “input.txt” from some random C: drive directory!
(Some of your students might not actually use Windows, and even if they do, they
might not have rights to that directory!)
3 • Don’t teach students to
using namespace std;
.
It’s been nearly 20 years now of people saying “don’t do that”...
Here’s how you open a file stream and use it:
1 2 3 4 5 6 7 8 9 10
|
int main()
{
std::ifstream input( "input.txt" ); // assume file is in same directory as exe, tyvm.
std::string s;
while (input >> s) // loop on success or failure of attempt to read data from file!
{
if (palindrome( s )) std::cout << "\"" << s << "\" is a palindrome.\n";
else std::cout << "\"" << s << "\" is NOT a palindrome.\n";
}
}
|
But, like I said, you should totally prefer to pipe input. This allow you to use files
OR type things in by hand.
1 2 3 4 5 6 7 8 9
|
int main()
{
std::string s;
while (std::cin >> s)
{
if (palindrome( s )) std::cout << "\"" << s << "\" is a palindrome.\n";
else std::cout << "\"" << s << "\" is NOT a palindrome.\n";
}
}
|
C:\Users\Fonzie\prog\hw1> copy con input.txt
apple
madam
racecar
11/11/11
not a palindrome
aibohphobia anyone?
^Z
C:\Users\Fonzie\prog\hw1> pal.exe < input.txt
"apple" is NOT a palindrome.
"madam" is a palindrome.
"racecar" is a palindrome.
"11/11/11" is a palindrome.
"not" is NOT a palindrome.
"a" is a palindrome.
"palindrome" is NOT a palindrome.
"aibohphobia" is a palindrome.
"anyone?" is NOT a palindrome.
C:\Users\Fonzie\prog\hw1> pal.exe
deified
"deified" is a palindrome.
hannah montana
"hannah" is a palindrome.
"montana" is NOT a palindrome.
^Z
C:\Users\Fonzie\prog\hw1> _ |
Since he is only expecting you to matching over individual words, you can easily do a recursive function. You have the right idea: compare the first and last characters, then recurse on everything between. You can do it much more succinctly though. Also, don’t forget to watch out for letter case:
1 2 3 4 5 6
|
bool palindrome( std::string s )
{
if (s.size() < 2) return true;
if (std::tolower( s.front() ) != std::tolower( s.back() )) return false;
return palindrome( s.substr( 1, s.size()-2 ) );
}
|
Normally in the real world, however, palindromes:
• ignore everything except letters and digits, and
• may consist of entire phrases.
You can improve things by updating main() to use
getline()
and fixing the palindrome function to ignore everything but alpha-numerics:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <cctype>
#include <ciso646>
#include <iostream>
#include <string>
bool palindrome( std::string s )
{
// zero or one characters → yes
if (s.size() < 2) return true;
// both front and back must be alphanumeric
if (!std::isalnum( s.front() )) return palindrome( s.substr( 1 ) );
if (!std::isalnum( s.back() )) return palindrome( s.substr( 0, s.size()-1 ) );
// if front != back → no
if (std::toupper( s.front() ) != std::toupper( s.back() )) return false;
// yes?
return palindrome( s.substr( 1, s.size()-2 ) );
}
int main()
{
std::string s;
while (getline( std::cin, s ))
{
if (palindrome( s )) std::cout << "\"" << s << "\" is a palindrome.\n";
else std::cout << "\"" << s << "\" is NOT a palindrome.\n";
}
}
|
C:\Users\Fonzie\prog\hw1> pal.exe
Aibohphobia
"Aibohphobia" is a palindrome.
Taco cat
"Taco cat" is a palindrome.
A man, a plan, a canal, PANAMA!
"A man, a plan, a canal, PANAMA!" is a palindrome.
yep
"yep" is NOT a palindrome.
^Z
C:\Users\Fonzie\prog\hw1> _ |
Hope this helps.