backward reader program

Pages: 12
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>

using namespace 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.
Give me a break... the word radar is symmetric!!!! Oo

Are you kidding me?
@athat
I did something like that with 2 for cycles but it didn't work:
1
2
3
4
5
6
7
8
9
10
11
for (int i = word.size() - 1; i >= 0; i--)
    {
           for(int j = 0; j <= word.size() - 1; j++)
           {
               cout << word[i];
               if(word[j] == word[i])
               {
                     cout << "palindrome";
               }
           }
    }

Any ideas?
Last edited on
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?
What about:
"word[i] == word[j] - word[i]" ?
Don't really get it!
Last edited on
Not really, subtracting characters from each other certainly makes little sense.
Answer the four questions for now.
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.
Of course you can tell. word.size()-1 is the last character, for example.
Oh i thought you gave an idea!
The first one is 0 the last one is "unknown"


The first is 0. The last is word.length()-1.



Sorry, got it wrong. I thought you wanted a number. Yes you are right cire.
I don't get it, athar asked, somethink he knew. LOOOOL
Any help?
Last edited on
Oh come on. Now what's the second to last character? And then in more general terms, what is the i-th to last character?
word.length()-2 second last character
word.length()-3 3rd to the last character
and so on
Any help?
Last edited on
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.
So you mean:
radar

word.length()-i == word[i] ??????
Not quite, but we're getting closer.
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.
i made it with a for loop.
but maybe like this:
int i;
int j = j-i;
?
Last edited on
You'd find it simpler if you defined a variable to represent the index of the first character


int i = 0; // first character is 0, remember?


and another for the index of the last character


int j = word.length() - 1;// remember?

Then:

1
2
3
4
 while ( i < j )
{
     // ...
}


You could use a for loop, but while is a better fit.
Tried it but didn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int j = 0;
    int i;
    cout << "Enter a word or a sentence: ";
    string word;
    getline(cin, word);
    for (i = word.size() - 1; i >= 0; i--)
    {
        cout << word[i];
    }
    while(j < i)
    {
        cout << "palindrome";
    }
    cout << "\n\n";
    main();
}
Pages: 12