I need help with a programing assignment for my CS 2 class, the task is to check rather a user inputted string is a palindrome meaning it is the same forwards as it is backwards. I have most of the program working, I input a string then copy it and reverse it and I got to the part were I need to compare the reversed string with the original and am getting an error message I don't understand. Here is my code so far:

#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

//class implimentations.
//******************************************************************************
class pstring
{
    private:
        string testString;
        int sizeOfstring;
        //size variable is needed to know how many characters are in
        //the string so that it can be transversed via loops.
        
        string *ptr;
        //creats a pointer to be used with the new operator to 
        //create a new string array to hold a reversed copy of the 
        //original string.
        
    public:
                //***CONSTRUCTOR***
        pstring(string input, int size)
        {
            testString=input;
            sizeOfstring=size;
        } 
          
        //*****OBJECT'S MEMBER FUNCTIONS PROTOTYPES*****
        bool isPalindrome();
        //check function that compars elements in string and sees if
        //they are equal.
        
        void pstring::copyString();
        //Using dynamic memory creates a second string to serve as the
        //destination of the reversed string.
        
};
//******************************************************************************
//Function prototypes
void gatherInput(string &input, int &size);
//Instructs the user about what they are supposed to input and allows them
//to input the string of their choice and the size.

void output(bool const &palindrome);
//******************************************************************************
//******************************************************************************
int main()
{
    string input;
    int size;
    bool palindrome;
    
    gatherInput(input, size);
    
    pstring one(input, size);
    //creates a pstring object for use in testing of the class.
    
    one.copyString();
    
    palindrome=one.isPalindrome();
    
    //output(palindrome);
    
    return 0;   
}
//******************************************************************************
//******************************************************************************
//Function implimentations
bool pstring::isPalindrome()
{
    unsigned int truth[sizeOfstring];
    //an array to hold the true false values calculated for each element
    //in the string.
    
    char var1, var2;
    //the char variabls are used to hold an element in each of the arrays while
    //it is being compared.
    
    for(int n=0; n< sizeOfstring; n++)
    {
        var1=ptr[n];
        var2=testString[n];
        
        if(var1==var2)
        {
           truth[n]=1;
        }
        else if(var1!=var2)
        {
            truth[n]=0;
        } 
        else
        {
            cout << endl << "An error has occured." << endl;
            //this line is just a catch in case some how the value for the
            //string is not catched in either of the first part of
            //the conditional statment.   
        }
    }
    
}
//******************************************************************************
void pstring::copyString()
{
    ptr= new string[sizeOfstring];
    //creates the second aray the size of user string.
    
    for(int n=0; n< (sizeOfstring); n++)
    {
        ptr[n]=testString[sizeOfstring-(n+1)];
    }
    
    for(int n=0; n< sizeOfstring; n++) //test print function.
    {
        cout << ptr[n] << "  " << testString[n] << endl;   
    }
}
//******************************************************************************
//Non-class function implimentations
void gatherInput(string &input, int &size)
{
    cout<< "Please enter the string you would like to test " << endl;
    cout<< "to see if it is a Palindrome." << endl;
    cout << "The string entered should have no spaces and should be" << endl;
    cout << " a single word." << endl;
    cin >> input;
    cout << endl;
    //Instcuts the user in the guidlines of the string input and allows them
    //to enter the string of their choice.
    
    cout<< "Pleae count the number of letters in the string you just entered ";
    cout << endl << "and enter the number now: ";
    cin >> size;
    
    cout << endl << endl << endl;
}
//******************************************************************************
void output(bool const &palindrome)
{
    cout << endl << "The string entered ";
    if(palindrome==true)
    {
        cout << "is a palindrome." << endl;
    }
    else if(palindrome==false)
    {
        cout << "is not a palindrome." << endl;
    }
    else
    {
        cout << endl << endl << "An error has occured." << endl;
        cout << "It was not possible to determin rather or not ";
        cout << "the value entered was a palindrome or not.";
    }
}
//******************************************************************************

The error is in line 83.

cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char' in assignment

I don't know how I'm supposed to compare the two variables, before I tried the character approach I tried doing it with brackets with the individual elements in each string and that gave error messages too.

note: I read ahead in the book were using and search the internet and I know that there are specialized built in functions for working with strings but I don't think were supposed to use those yet.

Thanks for the help.

Recommended Answers

All 8 Replies

Don't us char, use string

#include <iostream>
#include <string>

using namespace std;

bool is_pal(string a)
{
     int b=a.length();
     for(int i=0;i<(b/2);++i)
             if(a[i]!=a[b-i-1])return false;
     return true;
}

int main()
{
    string s;
    cout<<"Please enter a string:\n";
    cin>>s;
    if(is_pal(s)) cout<<"This is a palindrome.\n";
    else cout<<"This is not a palindrome.\n";
    
    return 0;
}

OK I tried changing the char part to string here is the code now.

string var1, var2;
    //the char variabls are used to hold an element in each of the arrays while
    //it is being compared.
    
    for(int n=0; n< sizeOfstring; n++)
    {
        var1[n]=ptr[n];
        var2[n]=testString[n];

and it gives the error message
cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char' in assignment

I also tried it with out the array brackets and got errors as well.

Also, I would like to fix my own code not use new code like the second comment, I appreciate it but I only kinda understand it and the teacher would never believe it was my own work if I turned that in.

#include <iostream>
#include <string>

using namespace std;

bool is_pal(string a)
{
     int b=a.length();
     for(int i=0;i<(b/2);++i)
             if(a[i]!=a[b-i-1])return false;
     return true;
}

int main()
{
    string s;
    cout<<"Please enter a string:\n";
    cin>>s;
    if(is_pal(s)) cout<<"This is a palindrome.\n";
    else cout<<"This is not a palindrome.\n";
    
    return 0;
}

frogboy, please don't post complete solutions to students' questions. Instead, in the rare cases like this where they ask a specific question, help them sort out the question rather than giving them a completely different solution from the one they're working on, especially without any explanation whatsoever why you're doing so.

Kyle,
Your ptr variable is a pointer to a string, not a string itself. So if you assign it by allocating new space using the new operator, you need to dereference it back to a string (rather than a string-pointer) before indexing characters out of it:

...
var1[n]=(*ptr)[n];
...

That said, it looks like you're doing a lot of needless extra work that the std::string class does for you, such as passing a size value into your constructor. A string already knows its own length:

string s = "My string";
cout << "The length of '" << s << "' is " << s.length() << endl;

And you probably don't need a pointer at all. Instead, just add another string member called "reversedString", rename your copyString() method to reverseString() (since that's what it does), and inside it, just start out with:

reversedString = testString;

to make the reversedString the correct length. Or there's a cool constructor which makes an n-character string pre-filled with a designated character:

reversedString = string(testString.length(), '*');

and then proceed as before.

Sorry it was pretty late. The code was not intended to be handed in as is. The point i was trying to convey was that there seemed to be a lot of unnecessary work being done. Basically you only need to check the first character of the string against the last, then the second against the second last etc.. until you reach the middle.

Thank you raptr_dflo I used the pointer advice you gave me and it fixed the problem I was having but that introduced new ones, so I used the length function you were talking about and with some extra work I completed the assignment. So thanks.

frogboy77 thank you for the explanation of your code, I chose not to use any of it because like I said it was a little too advanced for the class I'm in, but thanks anyway.

Thank you to all who helped me with this problem.

it was a little too advanced for the class I'm in

I doubt it. I know nothing of classes or pointers so your code is beyond me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.