I'm working on a function that uses recursion in order to delete duplicate characters in a string. Problem is, I'm not sure how to keep passing a string along in order to keep comparing adjacent characters without cutting the string somehow. Here's what I have so far:
string stringClean(const string& str)
{
string s1 = str;
if (/*first char == next char*/)
s1.at(/*first char*/) = "";
return stringClean(s1);
else
return s1;
}
As an example, stringClean("yyzzza") should return "yza". Any tips on how I should proceed?
if (str[0]==str[1]) return str[0]+stringClean(str.substr(2)); else return str[0]+stringClean(str.substr(1));Terminating condition is left as an exercise for the reader.