devo invertire una stringa in modo ricorsivo
nn so errore tira fuori, vi posto codice e errore
codice:
#include <iostream>
#include <string>
using namespace std;
string b;
string a1;
string invert(string a)
{
b = b + a.substr(a.length() - 1, 1);
if (a.length() == 1)
return b;
else
a1 = a.substr(0, a.length() - 2);
invert(a1);
}
int main()
{
string a;
cin >> a;
cout << invert(a) << endl;
return 0;
}
errore
codice:
bash-2.05b$ ./inv
ciao
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Abortito