This lab will help you practice with strings.
Write a program to determine if the user's input is a palindrome or not. Recall that a palindrome is a word which is the same when spelled forward or backward:
deed radar mom Otto 2112 racecar
(I didn't say they had to make sense...)
The only trick to palindromes is remembering to ignore capitalization.
In another text (or was it on the InterWebs..?) we find them creating the reverse of the string and comparing this to the original. It may seem clever at first, but it isn't a good approach. Neither is the common part of the solution to make the string all lower-case before comparing. (Nor is removing all the punctuation/spacing by duplicating what isn't into a copy of the string when doing phrase-style palindromes...not that that would concern us. *shrug*)
All of these suffer from two problems: duplicate storage of essentially the same information when we aren't going to ever need it again and processing the user's string multiple times to decide if it is a palindrome or not. (Some suffer that last problem in spades!)
You'll need to remove all these inefficiencies in your solution. You should only have to make one pass through the string to determine its palindromicity (is that even a word?? *shrug*).
Ideally, your code for determining palindrom-ness would be encapsulated within a function — making the main little more than a nice driver. However, I suppose that could be an option. *sigh*
How can you access individual characters within a string?
How can you tell how long a string's content is?
How can you tell that the characters at the beginning and end of a word are the same?
What do you do with odd length words?
How many characters do you have to look at to determine a word's 'palindromicness' if it contains 10 characters? 11 characters? n characters?
Does your program consider 34743 a palindrome? Why/Why not?
This assignment is (Level 2.5).
Add (Level 1.5) to make your palindrome determining code into a function. Just like the books', your function to determine palindrome-ness should return a bool — true if it is a palindrome or false if not. (Remember that moving that code into a function will turn your main into a simple driver program!)
(You could actually take this option a second time to make a function of your phrase checking code, too. The second time is only worth (Level 0.5), though. Sorry...don't worry, there is more level-y goodness below!)
Add (Level 1.5) to check for phrase palindromes as well as word palindromes.
What's a phrase palindrome? Well, like word palindromes, they ignore the capitalization of the letters. But, in addition, they ignore the presence of any spacing and punctuation as well. Like so:
Able was I ere I saw Elba. I Love Me, Vol. I. Madam, I'm Adam. A man, a plan, a canal, Panama. Rats live on no evil star.
You may want to use a constant helper string like this one:
const string punct = "?:;.!'\",`~@#$%^&*()-_+=|\\}{][/><";
. But the relevant cctype function works just as well. Similarly for spacing...
Some people like to automatically check the user's input for both ways, but that is really quite silly. If the user enters a phrase, it can't possibly be a word palindrome! (And vice versa...) So determine what kind of check to perform based on the user's data: if they enter a single word, check if it is a word palindrome; if they enter a phrase, check if it is a phrase palindrome. (If you are placing the palindrome checking code in a function, this word-vs-phrase determining code should stay in the main to decide which function to call.)
Comparing the phrase algorithm to your original word palindrome checker, is there really much difference? I'd be willing to bet the loop head is identical and only the position update(s) need(s) to change... (But perhaps I've said too much already...)
How can you detect that the user's input is a single word or a whole phrase? (Hint: What does one have that the other doesn't? And how can you look for that within a string?)
Add (Level 1.5) to merge your two palindrome-checking functions together by adding a bool argument which is true if the caller wants you to detect phrase palindromes and false if they merely want you to consider word palindromes. If the caller provides you a phrase candidate but requests word-style checking, act as requested. (In other words, 'Rise to vote, sir.' would not register as a palindrome when checked as a 'word'.) (Sorry about the pun...couldn't resist! *chuckle*)
You can even add another (Level 1) by making this new argument have a default value. (It should default to 'word' style checking.)
Add (Level 1.5) to place your palindrome checking function(s) in a library. (Adding it to the strextra library you worked on before would get you an extra (Level 0.5).)