Remove Consecutive Duplicates Recursively giving infinite recursion
Remove Consecutive Duplicates Recursively giving infinite recursion
Not a homework question.
I am self learning.
I have to remove consecutive characters in a string by recursion. However the program I made is not working for inputs containing duplicates. It is goining in infinite recursion and hence gives segmentation fault. However it is working for inputs which doesn't have consecutive duplicates in them. I have tried debugging in Eclipse Ide but things get weird when I debug. (I know how to debug) but I can't figure out the things are different when I debug and when I run. I will give you example after my code.
#include <iostream>
#include <cstring>
using namespace std;
void removeConsecutiveDuplicates(char *input) {
int l = strlen(input);
if(l == 0) {
return;
}
if(input[0] != input[1]) {
removeConsecutiveDuplicates(input+1);
return;
}
int i = 1;
for(; input[i] != ''; ++i) {
input[i-1] = input[i];
}
input[i] = '';
removeConsecutiveDuplicates(input);
}
int main(void) {
char ch[1000];
cin >> ch;
cout<<"The String Before Removing Duplicates : "<<ch<<endl;
removeConsecutiveDuplicates(ch);
cout<<"The String After Removing Duplicates : "<<ch<<endl;
return 0;
}
When I am debugging this code I and seeing the variable l value it is 16. This doesn't happen while running. What I am missing here?
std::string
char ch[1000]
Input : abc O/p : abc Input : aabccde O/P : abcde
– Brij Raj Kishore
Jul 2 at 9:52
The recursive call is not really helping your code here. You could just loop around. Recursion should be avoided is possible. If you are doing it as an exercise then I would replace the for loop with a memmove. The loop looks a bit strange. Usually the initialisation of i is after the for. I would expect a while here instead. Possibly a do while. Is the writing of the null char required as it is a condition of exiting the loop anyway. Try adding if(input[i] != ''), I suspect it is never triggered and can be removed.
– William J Bagshaw
Jul 2 at 10:04
c++:
string text; cin >> text; cout << "Before : " << text << "n"; text.erase(std::unique(text.begin(), text.end()), text.end()); cout << "After : " << text << "n";
– user1810087
Jul 2 at 10:08
string text; cin >> text; cout << "Before : " << text << "n"; text.erase(std::unique(text.begin(), text.end()), text.end()); cout << "After : " << text << "n";
In C++, you may do
*std::unique(input, input + strlen(input)) = '';
.– Jarod42
Jul 2 at 10:09
*std::unique(input, input + strlen(input)) = '';
3 Answers
3
This line :
input[i] = '';
doesn't do anything (input[i]
already has that value, because that was the end condition for the loop). You then call the removeConsecutiveDuplicates
function recursively, but the length of the string is still the same, so you'll keep making recursive calls until a stack overflow happens.
input[i]
removeConsecutiveDuplicates
Instead, you need to make the string length smaller :
input[i - 1] = '';
to avoid this infinite recursion.
To pass your self-assigned homework problem :), explain why the following line never has any effect in your code:
input[i] = '';
Got it here. this line is just doing nothing change
– Brij Raj Kishore
Jul 2 at 10:04
In your function, change this:
input[i] = '';
to this:
input[i-1] = '';
since you have chopped away a character, thus you should decrease the size of your string.
Without that, you wouldn't reduce the size, thus looping over and over again the same size, without it changing, would result in an infinite loop (since the size wouldn't change).
I suggest you use an std::string
next time, since this is C++.
std::string
But why this is working here? geeksforgeeks.org/remove-consecutive-duplicates-string I have just replaced the definition of function with the above website answer and it is working fine
– Brij Raj Kishore
Jul 2 at 10:02
You are right @SanderDeDycker, sorry.
– gsamaras
Jul 2 at 10:05
@BrijRajKishore I was wrong, sorry, please see my updated answer. +1 for your question.
– gsamaras
Jul 2 at 10:06
You are right in this. I will use string. I think that is the reason I am getting weird The String Before Removing Duplicates : 51-thread-select When I am debugging
– Brij Raj Kishore
Jul 2 at 10:30
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is the input of your code? You should really use
std::string
instead ofchar ch[1000]
in C++.– gsamaras
Jul 2 at 9:51