[ create a new paste ] login | about

Link: http://codepad.org/VQAfzZC2    [ raw code | output | fork ]

johannes - C++, pasted on Nov 8:
#include <iostream.h>

void reverse(char *str, unsigned int n)
{
    if (n < 2) return;
    int x = 0;
    unsigned int nn = n >> 1;
    while (n-- > nn)
    {
        char c = str[x];
        str[x++] = str[n];
        str[n] = c;
    }
}

std::string reverse(const std::string &str)
{
    int t = str.size();
    char *copy = new char[t + 1];
    str.copy(copy, t);
    reverse(copy, t);
    copy[t] = '\0';
    std::string r(copy);
    delete[] copy;
    return r;
}

int main()
{
    std::string r = reverse("collaboration");
    std::cout << r << std::endl;
    return 0;
}


Output:
1
noitaroballoc


Create a new paste based on this one


Comments: