Hi all!
I made a program which reads backwards, but i don't know how to make it recognize palindromes. Meaning if i type in radar, it says palindrome.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
cout << "Enter a word or a sentence: ";
string word;
getline(cin, word);
for (int i = word.size() - 1; i >= 0; i--)
{
cout << word[i];
}
cout << "\n\n";
main();
}
Well, you need to compare the first character with the last, the second character with the second last and so on.
You're not allowed to call main manually, by the way.
That checks if all characters are equal to all other characters, so that can't be right.
You don't need any nested loops.
What is the index of the first character? What's the index of the last character?
What is the index of the second character? What's the index of the second to last character?
Oh i thought you gave an idea!
The first one is 0 the last one is "unknown"
I don't know what the user will type.
It can even be nothing at all! :D
There is nothing i can tell.
Maybe couting a palindrome after 3 characters.
If (i >= 3); this way.
Exactly. And like I said, a word is a palindrome if the first character is equal to the last one, the second to the second to last, in short the i-th character must be equal to the i-th to last character.
You'd find it simpler if you defined a variable to represent the index of the first character and another for the index of the last character, then go from there.