I wrote this code to remove all occurrences of x from the string using recursion
#include <bits/stdc .h>
using namespace std;
void removex(string str)
{
if (str.length()==0)
{
return;
}
if (str[0] != 'x')
{
removex(str.substr(1,str.length()));
}
int i = 1;
for (; str[i] != '\0'; i )
{
str[i-1]=str[i];
}
str[i - 1] = str[i];
removex(str);
// cout<<"strq"<<str<<endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
removex(str);
cout << str << endl;
}
return 0;
}
however it's pass by value and If I try using pass by reference it gives an error as initial value of reference to non-const must be an lvalueC. which means I need to make the reference constant which is not suitable for rest of the code. I tried pass by pointer and using arrow operator however unable to get value at index and not sure how to make recursion call. to pass address or ponter? can someone modify it accordingly?
CodePudding user response:
Given that you are using C , I would use the features of the standard library. I believe this problem can be easily solved with a single line of code. Assuming that the string variable is called line, you would just need to do something like:
line.erase(remove(line.begin(), line.end(), 'x'), line.end());
Following is a complete example:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
std::string line = "12djd V x jhrf h58HSFH HUHFuhfdhkdh uhdfvygh 234 fhj xxx";
std::cout << "Line before removing the x character: " << line << std::endl;
line.erase(remove(line.begin(), line.end(), 'x'), line.end());
std::cout << "Line after removing the x character: " << line << std::endl;
return 0;
}
The example above would produce the following output:
Line before removing the x character: 12djd V x jhrf h58HSFH HUHFuhfdhkdh uhdfvygh 234 fhj xxx
Line after removing the x character: 12djd V jhrf h58HSFH HUHFuhfdhkdh uhdfvygh 234 fhj
An example that you can run is available here: https://onlinegdb.com/4wzMXTXP5
CodePudding user response:
Doing this with std::string
and recursion is a formidable template for insanity. The erase/remove idiom exists for just this purpose, functions iteratively, and is highly efficient. Best of all, it already exists; all you have to do is set up the calls.
That said, if you're bent on doing this recursively (and inefficiently) you need to convey the result back to the caller (including the recursive calls) somehow. The following does that using the function return type, which is std::string
. This also uses the global free operator
that allows concatenation of a char
std::string
to return a new string:
#include <iostream>
#include <string>
std::string removex(std::string str)
{
if (!str.empty())
{
if (str[0] == 'x')
str = removex(str.substr(1));
else
str = str[0] removex(str.substr(1));
}
return str;
}
int main()
{
std::string str = "Remove all x chars from this string.";
std::cout << "Before: " << str << '\n';
std::cout << "After: " << removex(str) << '\n';
return 0;
}
Output
Before: Remove all x chars from this string.
After: Remove all chars from this string.
That said, that isn't the way I'd do this. I'd use the erase/remove idiom which would be much faster, and much more memory efficient.