I have been learning about recursions and i did get the concept as to how they work and how to code them.Now,normal recursions are easy to visualize but when recursions involve loops they are totally baffling.
Let's head to the question first,Suppose,i have a string and i have to find possible no of Valid IP addresses that can be generated with that string.
Example
11211
Possible IP addresses
1.1.2.11
1.1.21.1
1.12.1.1
11.2.1.1
I tried to achieve this with something like the given code
vector<string> v;
string period(string str,int i){
size_t pos=str.find_last_of(".");
if(pos==string::npos)
str.insert(i+1,".");
else
str.insert(pos+2,".");
v.push_back(str);
return str;
}
void recursion(string str){
for(int i=0;i<=2;i++)
{
cout<<i<<'\n';
string strx=period(str,i);
if(count(strx.begin(),strx.end(),'.')<4)
recursion(str);
}
}
bool validIPaddress(string str){
//return true for valid ip address
}
int main () {
string str("11211");
recursion(str);
for(vector<string>::iterator it=v.begin();it!=v.end();++it){
if(validIPaddress(*it))
cout<<*it<<'\n';
}
}
I have made a picture of what i am trying to do with my logic.
Try every possible place where i can put my . in between strings and keep that every possible combination inside a vector which has been declared globally as well as don't put more than 3 periods inside a string.
After the recursion finishes ,i have every possible combination using periods and i can just loop through that vector and find the ones that are valid.
Now , the problem is that when i try to implement this logic , my output is not as i expected it to be.I tried to debug my code and get a glance over what have i been doing wrong but i am getting nowhere.
How can i achieve the solution of above problem using recursions?
Regards,
BOTJr.

recursive()calls itself, it passes the same value that it received. So it will recurse infinitely.