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
54 changes: 48 additions & 6 deletions CaesarCipher/CaesarCipher/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ @interface CaesarCipher : NSObject
- (NSString *)encode:(NSString *)string offset:(int)offset;
- (NSString *)decode:(NSString *)string offset:(int)offset;

@end
// Write a method called codeBreaker, which accepts two cipher strings as paramaters and returns a boolean value
- (BOOL) isEqual:(NSString *)first other:(NSString * )second;

@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];
NSString *str = [string lowercaseString]; //method invocation
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];
Expand All @@ -39,22 +41,62 @@ - (NSString *)encode:(NSString *)string offset:(int)offset {
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)];
}

//comparison method
- (BOOL) isEqual:(NSString *)first other:(NSString * )second {
if ([first length] != [second length]) {
return NO;
}

// which tells us whether they are actually the same input message encoded using two different offsets

// decode cipherstring2 using offsets from 0 to cipherstring1.length


for (int i = 0; i < first.length; i++) {
NSString *decoded = [self decode:second offset:i];

if ([decoded isEqualToString:first]) {
return YES;

}

}
return NO;
}

@end


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


CaesarCipher *str = [[CaesarCipher alloc] init];
// come up with 2 strings and 2 offsets, run codebreaker on both
NSString *example = @"do you like donuts?";
NSString *cipher = [str encode:example offset:2];
NSString *reverse = [str decode: cipher offset:2];

NSLog(@"%@", example);
NSLog(@"%@", cipher);
NSLog(@"%@", reverse);

if ([str isEqual: cipher other:[str encode:@"my name is charles" offset:2]]) {
NSLog(@"Yes");
}
else {
NSLog(@"No");
}
}
}
81 changes: 77 additions & 4 deletions Person/Person/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ - (NSString *)city;
- (void)setPhoneNumber:(NSString *)phoneNumber;
- (NSString *)phoneNumber;

-(void)registerAChild;




@end

@implementation Person {
NSString *_name;
NSString *_phoneNumber;
NSString *_city;
NSMutableArray* _children;
}

- (void)setName:(NSString *)name {
_name = name;
- (void)setName:(NSString *)newName {
_name = newName;
}

- (NSString *)name {
Expand All @@ -51,13 +57,80 @@ - (NSString *)phoneNumber {
return _phoneNumber;
}

- (void)setNameToThisPersonsName:(Person *)otherPerson {

otherPerson.name =_name;

}

-(Person*)registerChild{

Person* abc = [[Person alloc] init];
[abc setCity:[self city]];
[abc setPhoneNumber:[self phoneNumber]];

return abc;
}


- (NSString *)returnName: (Person *)otherPerson{

return otherPerson.name;
}

- (BOOL)checkSameCity: (Person *)stranger{

BOOL isSameCity = [stranger.city isEqualToString: _city];

return isSameCity;
}




@end


int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");

//create sarah & set properties!!
Person *sarah = [[Person alloc] init];
[sarah setCity:@"Sao Paolo"];
[sarah setName:@"Sarah"];


//create Bereket & set properties!!
Person *Bereket= [[Person alloc] init];
[Bereket setCity:@"New York"];
[Bereket setName:@"BearCat"];



NSLog(@"%@", [Bereket name]);
NSLog(@"%@", [sarah name]);


Person *BereketsKid = [Bereket registerChild];

if (BereketsKid.city == Bereket.city & BereketsKid.phoneNumber == Bereket.phoneNumber) {
NSLog(@"Bearcats a Baby Daddy too");
}

[BereketsKid setName:@"rowen"];

//call methods to check if we from the same hood
BOOL comparableNotReally = [Bereket checkSameCity:sarah];

if(comparableNotReally == YES){
NSLog(@"LIVE IN THE SAME CITY!");
}
else{
NSLog(@"DONT LIVE IN THE SAME CITY! D:");
}



}
return 0;
}
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***

//donuts