Hi,
I'm trying to make my program produce every possible letter combination, for example, with the word "one".
This is what I've got so far, I don't understand how to do it properly:
The following code produces one, noe, eon, one, which isn't right... any thoughts on what to do?
Kind Regards
I'm trying to make my program produce every possible letter combination, for example, with the word "one".
This is what I've got so far, I don't understand how to do it properly:
Code:
NSString *word = @"one";
NSString *newWord = @"";
for (int i=0;i<[word length];i++){
NSString *sub = [self getSubstring:word:i:i+1];
newWord = [self concantenateStrings:newWord:sub];
newWord = [self concantenateStrings:newWord:@" "];
}
NSArray *letters = [newWord componentsSeparatedByString:@" "];
for (int i=0;i<[letters count];i++) {
NSString *temp = [letters objectAtIndex:i];
for (int k=0;k<[letters count];k++) {
if (i != k) {
temp = [self concantenateStrings:temp:[letters objectAtIndex:k]];
}
}
NSLog(@"temp: %@", temp);
}
}
-(NSString*)getSubstring:(NSString *)substring:(int)start:(int)end {
NSString *returnString = [[substring substringToIndex:end] substringFromIndex:start];
return returnString;
}
-(NSString*)concantenateStrings:(NSString*)string1:(NSString*)string2 {
return [string1 stringByAppendingString:string2];
}
Kind Regards