I suspect your stack size is crossing its limit. If you are running it on Linux, do "limit" and see your stacksize. There are two ways to avoid this situation
1) (Not Recommended) Do "limit stacksize unlimited" (only if you are on unix based system). And run the program again.
2) (Recommended).
Change
void nextpermute(string& s){
int fc = firstchar(s);
int cc = ceilchar(s,fc);
swap(s,fc,cc);
sort(&s[fc]+1,&s[fc]+s.length()-fc);
if(s!="9876543210"){
cout<<s<<"\n";
nextpermute(s);
}
else
cout<<s<<"\n";
}
to
void nextpermute(string& s){
int fc = firstchar(s);
int cc = ceilchar(s,fc);
swap(s,fc,cc);
sort(&s[fc]+1,&s[fc]+s.length()-fc);
cout <<s<<"\n";
}
and modify your main function as
int main()
{
string s = "0123456789";
while (s != "9876543210")
{
nextpermute(s);
}
}
Above change will do away with the recursion of "nextpermute" and hence your stacksize limit will never be crossed