Last active
August 29, 2015 14:00
-
-
Save antsmartian/b52169137b790b63e030 to your computer and use it in GitHub Desktop.
Find the occurrence of a char in a string
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 <objc/objc.h> | |
#import <objc/Object.h> | |
#import <Foundation/Foundation.h> | |
@implementation TestObj | |
int main() | |
{ | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
// your code goes here | |
NSString *string1 = @"antoaravinth"; | |
NSString *string2 = @"test"; | |
NSString * alphabets = @"qwertyuioplkjhgfdsazxcvbnm"; | |
NSMutableDictionary *wordOccurence1 = [NSMutableDictionary dictionary]; | |
NSMutableDictionary *wordOccurence2 = [NSMutableDictionary dictionary]; | |
for (NSInteger charIdx=0; charIdx<alphabets.length; charIdx++) | |
{ | |
NSString *s = [NSString stringWithFormat:@"%c",[alphabets characterAtIndex:charIdx]]; | |
NSUInteger numberOfOccurrences = [[string1 componentsSeparatedByString:s] count] - 1; | |
NSUInteger numberOfOccurrences2 = [[string2 componentsSeparatedByString:s] count] - 1; | |
if(numberOfOccurrences != 0) | |
[wordOccurence1 setObject:[NSString stringWithFormat:@"%d",numberOfOccurrences] forKey:s]; | |
if(numberOfOccurrences2 != 0) | |
[wordOccurence2 setObject:[NSString stringWithFormat:@"%d",numberOfOccurrences2] forKey:s]; | |
} | |
[wordOccurence1 enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) { | |
NSLog(@"%@ = %@", key, object); | |
}]; | |
NSLog(@"Dic is %@",wordOccurence1); | |
NSLog(@"Dic is %@",wordOccurence2); | |
[pool drain]; | |
return 0; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment