Reversing STRING

I'm trying to reverse any entered String and then proof if its a palindrome ( " madam" )

i'm running into some errors:

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
#include <iostream>
#include <string>
using namespace std;

void printReverse();

   int main()
     {
     string str ;
     cout << "Enter any STRING of characters: ", cin >> str ;
     void printReverse(string str );
     }

   void printReverse(string str )
   {

   for( int i = str.length()-1; i >= 0; --i )
    {
    cout << str[i];
    }
    while (string str == string str[i])
    {
    cout << " the string you intered is a palindrome" << endl;
    }

   }

Typically when you run into errors you want to take a look at the first one generated and see what you can do to fix it. Then re-compile. Rinse and repeat.

Failing that, you'll want to supply the actual text of the first few errors complete with line numbers so other people don't have to guess what errors you're talking about.

You might want to google : "How do I call a function in C++?", "How do I compare one character of a string to another in C++?" and "What is the scope of a variable declared in a for loop in C++?" When you've got those fixed you'll probably want to review the logic where you determine if the string is a palindrome, because that is wrong.
Last edited on
Line 11:
 
void printReverse(string str );

This is defining a function, also known as a function prototype.

 
void printReverse();

You're missing an argument in your function prototype.

 
while (string str == string str[i])

str is different from the one that is passed as an argument.

1
2
3
4
for( int i = str.length()-1; i >= 0; --i )
{
    cout << str[i];
}

You may want to declare another string, to store the reversed string.

http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.