Home:ALL Converter>Converting to C style String

Converting to C style String

Ask Time:2017-09-28T06:38:40         Author:Granzo

Json Formatter

I'm trying to create a function that takes in a string, the reverses it, which is all being done in main. Here's what I have so far.

#include <cstring>
#include <iostream>
#include <string>


std::string str = "zombie";


void Reverse( std::string a)
{

    char* a1 = a.c_str;
    char* a2;

    for (int i = (strlen(a1) -1); a1[i]!=0; i--)
    {
        a2 += a1[i];
    }

    str = a2;
}

int main()
{
    Reverse(str);

    std::cout << str << std::endl;
}

But i keep getting this error. I can't utilize pointers in this question. Any suggestions?

EDIT: I'm particularly having issues with converting the inserted parameter a into a c-style string.

EDIT 2: So I started cleaning it up a bit, to my understanding, the code that I had wouldn't accomplish my goal at all, even with proper changes. Would this be on a better track? I plan on just converting the string to c-style before passing it to the function.

void Reverse(const char* s)
{
    int x = strlen(s);
    std::string str = "";

    for (int c = x; c > 0; c--)
    {
        str += s[c];
    }

}

Author:Granzo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46458269/converting-to-c-style-string
yy