Created
September 13, 2015 18:29
-
-
Save 0x8badf00d/97135aeb7ea00d4d8a23 to your computer and use it in GitHub Desktop.
Program to count frequency of a word in sentence. (Stripping any
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
void frequencyCounterOfWordsInSentence(NSString *sentence) | |
{ | |
NSArray *words = [sentence componentsSeparatedByString:@" "]; | |
NSMutableDictionary *wordCounterDict = [NSMutableDictionary dictionary]; | |
for(NSString *word in words) | |
{ | |
// Remove any characters from word which is not from letter character set | |
NSString *scrubbedWord = [[word componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""]; | |
// Increment counter value that we retrieve from Hashmap. | |
NSNumber *count = wordCounterDict[scrubbedWord]; | |
NSUInteger counter = count.unsignedIntegerValue; | |
counter++; | |
[wordCounterDict setValue:@(counter) forKey:scrubbedWord]; | |
} | |
NSLog(@"%@",wordCounterDict); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
@autoreleasepool | |
{ | |
frequencyCounterOfWordsInSentence(@"Grammar. a grammatical unit of one or more words that expresses an independent statement, question, request, command, exclamation, etc., and that typically has a subject as well as a predicate, as in John is here. or Is John here? In print or writing, a sentence typically begins with a capital letter and ends with appropriate punctuation; in speech it displays recognizable, communicative intonation patterns and is often marked by preceding and following pauses"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment