Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First you need a method to check if one word is an anagram of a second word. There are many possible solutions (search for "Objective-C anagram"). This is essentially the method from <a href="https://stackoverflow.com/a/13465672/1187415">https://stackoverflow.com/a/13465672/1187415</a>, written slightly differently:</p> <pre><code>- (BOOL)does:(NSString*)longWord contain:(NSString *)shortWord { NSMutableString *longer = [longWord mutableCopy]; __block BOOL retVal = YES; // Loop over all characters (letters) in shortWord: [shortWord enumerateSubstringsInRange:NSMakeRange(0, [shortWord length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { // Check if letter occurs in longer word: NSRange letterRange = [longer rangeOfString:substring]; if (letterRange.location != NSNotFound) { // Yes. Remove from longer word and continue. [longer deleteCharactersInRange:letterRange]; } else { // No. Set return value to NO and quit the loop. retVal = NO; *stop = YES; } }]; return retVal; } </code></pre> <p>Examples:</p> <ul> <li><code>[self does:@"abandoned" contain:@"bond"] = YES</code></li> <li><code>[self does:@"abandoned" contain:@"sea"] = NO</code>, because there is no "s" in the first word.</li> <li><code>[self does:@"abandoned" contain:@"noon"] = NO</code>, because "noon" has 2 letters "o", but the first word has only one "o".</li> </ul> <p>Then you can proceed as follows:</p> <pre><code>NSArray *englishWords = ...; // Your array of english words NSString *inputWord = @"abandoned"; // The input string NSString *middleLetter = [inputWord substringWithRange:NSMakeRange([inputWord length]/2, 1)]; NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSString *word, NSDictionary *bindings) { // Word must have at least 4 letters: if ([word length] &lt; 4) return NO; // Word must contain the middle letter: if ([word rangeOfString:middleLetter].location == NSNotFound) return NO; // Word must contain only letters of the input word: if (![self does:inputWord contain:word]) return NO; return YES; }]; NSArray *matchingWords = [englishWords filteredArrayUsingPredicate:predicate]; NSLog(@"%@", matchingWords); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload