Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 58 additions & 29 deletions CaesarCipher/CaesarCipher/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,78 @@ @interface CaesarCipher : NSObject

- (NSString *)encode:(NSString *)string offset:(int)offset;
- (NSString *)decode:(NSString *)string offset:(int)offset;
- (BOOL)codeBreaker:(NSString *)stringA stringB:(NSString *)stringB;

@end


@implementation CaesarCipher

- (NSString *)encode:(NSString *)string offset:(int)offset {
if (offset > 25) {
NSAssert(offset < 26, @"offset is out of range. 1 - 25");
}
NSString *str = [string lowercaseString];
unsigned long count = [string length];
unichar result[count];
unichar buffer[count];
[str getCharacters:buffer range:NSMakeRange(0, count)];
char allchars[] = "abcdefghijklmnopqrstuvwxyz";

for (int i = 0; i < count; i++) {
if (buffer[i] == ' ' || ispunct(buffer[i])) {
result[i] = buffer[i];
continue;
}
char *e = strchr(allchars, buffer[i]);
int idx= (int)(e - allchars);
int new_idx = (idx + offset) % strlen(allchars);

result[i] = allchars[new_idx];
}

return [NSString stringWithCharacters:result length:count];
if (offset > 25) {
NSAssert(offset < 26, @"offset is out of range. 1 - 25");
}
NSString *str = [string lowercaseString];
unsigned long count = [string length];
unichar result[count];
unichar buffer[count];
[str getCharacters:buffer range:NSMakeRange(0, count)];
char allchars[] = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < count; i++) {
if (buffer[i] == ' ' || ispunct(buffer[i])) {
result[i] = buffer[i];
continue;
}
char *e = strchr(allchars, buffer[i]);
int idx= (int)(e - allchars);
int new_idx = (idx + offset) % strlen(allchars);
result[i] = allchars[new_idx];
}
return [NSString stringWithCharacters:result length:count];
}

- (NSString *)decode:(NSString *)string offset:(int)offset {
return [self encode:string offset: (26 - offset)];
return [self encode:string offset: (26 - offset)];
}

- (BOOL)codeBreaker:(NSString *)stringA stringB:(NSString *)stringB {

for (int i = 0; i < 25; i++) {


for (int j = 0; j < 25; j++) {


NSString *decodedStringA = [self decode:stringA offset:i+1];

NSString *decodedStringB = [self decode:stringB offset:j+1];

if ([decodedStringA isEqualTo: decodedStringB]) {
NSLog(@"Same");
return 1;
}

}

}
NSLog(@"different");
return 0;
}
@end


int main(int argc, const char * argv[]) {
@autoreleasepool {

}
@autoreleasepool {
CaesarCipher *cipher = [[CaesarCipher alloc] init];
[cipher codeBreaker:@"okmg" stringB:@"tpkg"];


}
}


2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ Write a program to simulate an election. Create a class called **VotingSimulator
4. Ask the ElectionManager to ***initiatePolling***
5. Follow the instructions on the console. After each round of polling you will be asked(within the console) whether you want to continue or not.
6. Ask the ElectionManager to ***displayResults***

Yeah, buddy!