The question is to shift x at last i am getting a segmentation error

#include<bits/stdc++.h>
using namespace std;

string XatLast(string str)
{
	if(str.size() == 0)
	{
		return "";
	}

	if(str[0] == 'x')
	{
		return XatLast(str.substr(1) + string("x"));
	}

	return XatLast(str[0] + str.substr(1));
}

int main()
{
	string str;
	cin >> str;
	cout << XatLast(str);
}

this is not correct approach

try to dry run this approach
you will get your mistake

lets take an example xxabc;
if str[0] is x then you are calling for the string xabcx;
then you call for abcxx;
now after that you are repeatedly calling for abcxx
size of string never become 0

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.