Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/b52169137b790b63e030 to your computer and use it in GitHub Desktop.
Save antsmartian/b52169137b790b63e030 to your computer and use it in GitHub Desktop.
Find the occurrence of a char in a string
#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