Hey guys, I've tried writing code to reverse my sentence, but I keep getting an error in my for loop saying "Expected Expression". Also, should the asterisk go after the "modify" in my code? I'm not sure how pointers are exactly supposed to work and I haven't been given any errors for doing so yet. Thanks in advance, and here is my code!

void f1 (const char* sentence)
{
    char sentence2;
    sentence = &sentence2;
    char* modify = &sentence2;

    basic_string <char>::reverse_iterator revSentence;
    cout << "The reversed string is:" << endl;

    for (revSentence = modify*.rbegin(); revSentence !=modify*.rend(); modify*++)
    {
        cout << *revSentence;
    }


}

line 10: You are treating modify as if it were of type std::string, it isn't. modify is of type char* and character arrays don't have methods like begin() and end().

lines 3-5: What is the purpose of those lines? You can't convert a single character as you declared on line 3 as a character array. The pointer sentence should already point to a string of characters. What you probably want to do is convert from char* to std::string

std::string modify = sentence;

modify is a char * not a string therefore it does not have rbegin() or rend() member function. Do you have to work with a char * or can you use a string? If you have to use a char * then you need to know the legnth of the string and then you can use a for loop to loop from the back of the string to 0. If you can use a string than you can use a reverse iterator.

We are to use another pointer in the function to print the string in reverse order, leaving the original string unaltered.

Thank you Ancient Dragon, and NathanOliver for your help! I will go and revise my code

call strlen() to find the length of sentence then set a pointer to that position. In a loop you will have to decrement instead of increment the pointer so that you get the string in reverse order.

If you're absolutely required to use a reverse_iterator, it's possible to construct a reverse_iterator to an array pointer and use that as you would any other reverse_iterator:

const char s[] = "Hello, World!";
size_t n = strlen( s );
std::reverse_iterator< const char* > rev_it(s + n);
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.