-5

Hi everybody I am c++ beginner I am working on program to examine a user entered string to determine if it's a palindrome. I have the most of the code but still two problems the first one is In function ‘bool PalindromeTest()’and the second error is: error: ‘strrev’ was not declared in this scope

#include <iostream>
#include <cstring>
using namespace std;

char s[100];
char sr[100];

void InputString();
bool PalindromeTest();
void PrintMessage();

int main()
{
       InputString();
       return 0;
}

void InputString(int n=0)
{
       cout<<"Input the string you wish to be tested";
       cin.getline(s, 100);
       if(strlen(s)>n)
          PalindromeTest();
}

bool PalindromeTest()
{
       bool rvalue = true;
       strcpy(sr, s);
       strrev(sr);
       if(strcmp(s, sr) == 0)
       {
              rvalue=true;
              PrintMessage();
       }
       else
       {
          rvalue=false;
          PrintMessage();
    }
void PrintMessage(bool rvalue)
{
       if(true == rvalue)
         cout<<"The entered string IS a palindrome"<<endl;
       else
         cout<<"The entered strins IS NOT a palindrome"<<endl;
}
Deanie
  • 2,316
  • 2
  • 19
  • 35

4 Answers4

0

Since this is a C++ question, how about a real C++ solution? With str being a std::string:

bool isPalindrome = std::equal(str.begin(), str.end(), str.rbegin());
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • I guess we could optimize that by replacing `str.end()` with `str.begin() + str.size() / 2` – M.M May 03 '14 at 03:42
  • Yeah, this isn't written for efficiency. But I thought it made for an elegant little one liner. If I needed an efficient solution, I would just loop and compare characters, C style (Edit: That would look very much like the answer @MichaelJ just posted). – Reto Koradi May 03 '14 at 03:46
  • @RetoKoradi - That is a good and simple solution, but I suspect that our questioner might not be ready to understand it. :-) – Michael J May 03 '14 at 03:56
  • @MichaelJ: It explains itself. It's a palindrome if reading the string in sequence from **begin** to **end** is **equal** to what you get if you **begin** reading in **r**-everse. ;) – Reto Koradi May 03 '14 at 04:53
  • @RetoKoradi - Our friend does not yet understand functions or local variables. Iterators and std:: algorithms are going to be tough. – Michael J May 03 '14 at 04:57
  • @MichaelJ: Sure. Frankly, fixing the original code, or creating a complete list of all the problems, was just not that interesting. So I thought I might as well have some fun by turning 50 lines of code into 1 line. And show how C++ concepts can be applied to the problem. – Reto Koradi May 03 '14 at 05:05
0

I don't think strrev() is a C++ function.

A quick way to identify a palindrome is to use a loop with two counters, say i and j.

Start with i=0 and j=strlen(str)-1 (the last valid character) and iterate ++i and --j, while i<j.

If, on any iteration, str[i] != str[i], then it is not a palindrome so you can return false. If it gets to the end of the loop without finding any difference, then it is a palindrome.

bool Palindrome(const char *str)
{
    int len = strlen(str);
    int i, j;
    for (i=0, j=len-1; i<j; ++i, --j)
        if (str[i] != str[j])
            return false;
    return true;
}
Michael J
  • 7,631
  • 2
  • 24
  • 30
  • this couse i should replace with what code ? from what line to line ?? – user3598206 May 03 '14 at 03:49
  • @user3598206 - Read your own post and try to figure that out. Did you write the code you posted? If you did, then I think you can figure that out. Have a try and post it. I will help you if it is wrong. – Michael J May 03 '14 at 03:54
  • int main() { InputString(); return 0; } void InputString(int n=0) { cout<<"Input the string you wish to be tested"; cin.getline(s, 100); if(strlen(s)>n) PalindromeTest(); } bool Palindrome(const char *str) { int len = strlen(str); for (i=0, j=len-1; i – user3598206 May 03 '14 at 03:59
  • i decleared i and j before the function palindrome and still says i and j was not decrealed – user3598206 May 03 '14 at 04:00
  • @user3598206 - Oops. I made a mistake, not declaring those. That'll teach me to write code without compiling it. It is now fixed. – Michael J May 03 '14 at 04:10
  • @user3598206 - I didn't spot your code in the comment above until just now. Without trying to be rude: I don't think you know enough C++ yet to do this program. I could give you a solution, but you won't learn like that. Do you have a C++ text book? Which one? I think you will benefit from revising the absolute basics before you go on. Until you understand how functions and parameters work, this is going to be pretty impossible. – Michael J May 03 '14 at 04:36
0

I fixed your code :

#include <iostream>
#include <cstring>
#include <cstdio> 
    #include <cstdlib>

using namespace std;

char s[100];
char sr[100];

void InputString();
bool PalindromeTest();
void PrintMessage(bool);

int main()
{
    InputString();

    std::cin.get() ;    
    std::cin.get() ;
    return 0;
}

void InputString()
{
    int n = 0;
    cout<<"Input the string you wish to be tested \n";
    cin.getline(s, 100);
    if(strlen(s)>n)
        PalindromeTest();
}

bool PalindromeTest()
{
    bool rvalue = true;
    strcpy(sr, s);
    strrev(sr);
    if(strcmp(s, sr) == 0)
    {
        rvalue=true;
        PrintMessage(rvalue);
    }
    else
    {
        rvalue=false;
        PrintMessage(rvalue);
    }
    return false ; // ??
}
void PrintMessage(bool rvalue)
{
    if(true == rvalue)
        cout<<"The entered string IS a palindrome"<<endl;
    else
        cout<<"The entered strins IS NOT a palindrome"<<endl;
}
  1. method signatures (name , number of parameters and their types) in forward declarations must be equivalent to their signatures in their definitions .(take a look at declarations and definitions of default and fixed versions of InputString , PrintMessage) .

  2. n was undefined .

  3. methods with type other than void must return a value , but in your case PalindromeTest does not need to return anything .

here is a better version :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

char s[100];
char sr[100];

void InputString();
bool PalindromeTest();
void PrintMessage(bool);

int main()
{
    InputString();

    std::cin.get() ;    
    std::cin.get() ;
    return 0;
}

void InputString()
{
    cout<<"Input the string you wish to be tested \n";
    cin.getline(s, 100);
    PrintMessage(PalindromeTest());
}

bool PalindromeTest()
{
    strcpy(sr, s);
    strrev(sr);
    if(strcmp(s, sr) == 0)
    {
        return true ;
    }
    else
    {
        return false ;
    }
}
void PrintMessage(bool rvalue)
{
    if(rvalue)
        cout<<"The entered string IS a palindrome"<<endl;
    else
        cout<<"The entered strins IS NOT a palindrome"<<endl;
}
ColonelMo
  • 134
  • 1
  • 9
0

You need to pass rvalue as parameter while calling the PrintMessage(). Think, it will help.

DUSMANTA
  • 33
  • 6