#include<iostream>
#include<string.h>
using namespace std;
string revString(string s,string copy,int i,int j)
{
if(i==s.length())
return copy;//if 'i' reaches to the length of the string it returns the copied value
else{
copy[i]=s[j];//copying the string s into copy
return revString(s,copy,++i,--j);// calling recursively
}
}
int main()
{
string s,copy;
cout<<"Enter a string without entering spaces\n";
cin>>s;
int i=0,j=s.length()-1;
cout<<revString(s,copy,i,j);//function for reversing the string
return 0;
}
here i am trying to copy the string 's' into the string 'copy' by using recursion but the function isn't returning anything.
Because you have not allocated memory to your copy
variable, but trying to assign values to it. I would suggest you to read more about memory allocation in C++ string
class.
With minimum alterations to your code, you can make it work by adding the following snippet just before calling your revString()
function:
copy.resize(s.size());