I found the solution. Here it is.
- (void)viewDidLoad {
[super viewDidLoad];
[self expand_combinations:@"abcd" arg2:@"" arg3:3];
}
-(void) expand_combinations: (NSString *) remaining_string arg2:(NSString *)s arg3:(int) remain_depth
{
if(remain_depth==0)
{
printf("%s\n",[s UTF8String]);
return;
}
NSString *str = [[NSString alloc] initWithString:s];
for(int k=0; k < [remaining_string length]; ++k)
{
str = [s stringByAppendingString:[[remaining_string substringFromIndex:k] substringToIndex:1]];
[self expand_combinations:[remaining_string substringFromIndex:k+1] arg2:str arg3:remain_depth - 1];
}
return;
}
The changes I made to make it work are in the line
NSString *str = [[NSString alloc] initWithString:s];
From there I replace every instance of s with str. It seems that by using s instead of str, some important value was getting overwritten which was causing the problem to not work correctly. It is necessary to create another NSString variable str so that the contents of s do not get overwritten.