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
151 changes: 145 additions & 6 deletions CaesarCipher/CaesarCipher/main.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//

// main.m
// CaesarCipher
//
// Created by Michael Kavouras on 6/21/15.
// Copyright (c) 2015 Mike Kavouras. All rights reserved.
//
// Created by Diana Elezaj 👩 on 6/21/15.
// Copyright (c) 2015 Diana Elezaj. All rights reserved.


#import <Foundation/Foundation.h>

@interface CaesarCipher : NSObject

- (NSString *)encode:(NSString *)string offset:(int)offset;
- (NSString *)decode:(NSString *)string offset:(int)offset;
- (BOOL) codeBreaker: (NSString *)string1 compareWith: (NSString *)string2;

@end

Expand All @@ -28,14 +29,14 @@ - (NSString *)encode:(NSString *)string offset:(int)offset {
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);
Expand All @@ -50,11 +51,149 @@ - (NSString *)decode:(NSString *)string offset:(int)offset {
return [self encode:string offset: (26 - offset)];
}

// check two strings before decoding them.
// If they complete these first sonditions, then decode, and compare if there is the same input message
BOOL checkTwoStrings (NSString *S1, NSString *S2, int off1, int off2) {

// check string's length, If they aren't the same length
// then we don't need to loop
NSInteger count = [S1 length];
if (count != [S2 length]) {
NSLog(@"Strings don't have the same length");
return YES;
}
//if strings are the same, but with different offsets
else if (S1 == S2 && off1!=off2) {
NSLog(@"Not the same input message! ");
return YES;
}
// if strings different, but with the same offset
else if (S1 != S2 && off1==off2) {
NSLog(@"Not the same input message! ");
return YES;
}
// if same strings, and same offset
else if (S1 == S2 && off1==off2) {
NSLog(@"You entered same strings, and same offsets! ");
return YES;
}
return 0;
}

// use codeBreaker only after decoding two trings
- (BOOL) codeBreaker: (NSString *)string1 compareWith: (NSString *)string2{
for (int i = 1; i <26; i++) {
//if decoded strings are equal
if ([[self encode: string1 offset:i] isEqualToString:[self encode:string2 offset:i]]) {
NSLog(@"Same input message!");
return YES;
break;
}
// if decoded strings are different
else if ([[self encode: string1 offset:i] isNotEqualTo:[self encode:string2 offset:i]]) {
NSLog(@"Not the same input message!");
return NO;
break;
}
}
return 0;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop will only ever execute 1 time at the most. In either situation you are calling return AND break. Since the 2 conditions you are checking are opposite, 1 will always be true.


@end


int main(int argc, const char * argv[]) {
@autoreleasepool {
CaesarCipher *checkStrings = [[CaesarCipher alloc] init];
char firstStr [30];
char secondStr[30];
int offset1, offset2, option, thatsDrivingMeCrazy;

NSLog(@"Choose an option:");
NSLog(@"1. Encode a string");
NSLog(@"2. Decode a string");
NSLog(@"3. Check two encoded strings");
scanf("%d", &option);

//Encode a string
if (option==1) {
NSLog(@"Please enter a string:");
scanf("%d", &thatsDrivingMeCrazy);
scanf("%[^\n]", firstStr);
NSLog(@"Offset:");
scanf("%d", &offset1);
if (offset1 > 25) {
NSLog(@"offset is out of range. 1 - 25");
return NO;
}
NSString *encodeAString = [NSString stringWithFormat:@"%s", firstStr];
NSString *encodedString = [checkStrings encode:encodeAString offset:offset1];
NSLog(@"Your sting encoded with offset of %d is %@", offset1, encodedString);

}

// Decode a string
else if (option==2) {

NSLog(@"Please enter a string:");
scanf("%d", &thatsDrivingMeCrazy);
scanf("%[^\n]", firstStr);
NSLog(@"Offset:");
scanf("%d", &offset1);
if (offset1 > 25) {
NSLog(@"offset is out of range. 1 - 25");
return NO;
}
NSString *decodeAString = [NSString stringWithFormat:@"%s", firstStr];
NSString *decodedString = [checkStrings decode:decodeAString offset:offset1];
NSLog(@"Your sting decoded with offset of %d is %@", offset1, decodedString);
}

// Check two encoded strings
else if (option==3) {

NSLog(@"Please enter a string:");
scanf("%d", &thatsDrivingMeCrazy); //I just had to add this line, in order to read the string
scanf("%[^\n]", firstStr);
NSLog(@"Offset:");
scanf("%d", &offset1);
if (offset1 > 25) {
NSLog(@"offset is out of range. 1 - 25");
return NO;
}

NSLog(@"Please enter another string:");
scanf("%d", &thatsDrivingMeCrazy); //I just had to add this line, in order to read the second string
scanf("%[^\n]", secondStr);
NSLog(@"Offset:");
scanf("%d", &offset2);
if (offset1 > 25) {
NSLog(@"offset is out of range. 1 - 25");
return NO;
}

NSString *firstWord = [NSString stringWithFormat:@"%s", firstStr];
NSString *secondWord = [NSString stringWithFormat:@"%s", secondStr];

// check the strings and offset before decoding them.
// checkTwoStrings(firstWord, secondWord, offset1, offset2);
BOOL checkedStringsBeforeDecoding = checkTwoStrings(firstWord, secondWord, offset1, offset2);

if (checkedStringsBeforeDecoding == 0) {

if (firstWord != secondWord && offset1 != offset2) {
//decode encodedFirstString
NSString *decodedFirstString = [checkStrings decode:firstWord offset:offset1];
NSLog(@"String %@ decoded with offset of %d is %@", firstWord, offset1, decodedFirstString);

//decode encodedFirstString2
NSString *decodedSecondString = [checkStrings decode:secondWord offset:offset2];
NSLog(@"String %@ decoded with offset of %d is %@", secondWord, offset2, decodedSecondString);

//check decoded Strings if they are the same
[checkStrings codeBreaker:decodedFirstString compareWith:decodedSecondString];
}
}
}
}
}
}
65 changes: 61 additions & 4 deletions Election/Election/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ - (void)displayResults;

@end


// Election class
@implementation Election {
NSString *_electionName;
Expand Down Expand Up @@ -112,7 +111,6 @@ - (void)displayCandidates {
}
}


- (void)displayResults {
printf("\n%s\n", [_electionName UTF8String]);
for (Contender *c in _listOfContenders) {
Expand Down Expand Up @@ -151,7 +149,6 @@ - (void)vote {

@end


// ElectionManager class
@interface ElectionManager : NSObject

Expand Down Expand Up @@ -198,14 +195,74 @@ - (BOOL)pollsOpen {
return answer != 0;
}

@end

@interface VotingSimulator : NSObject

-(void)simulate;

@end

@implementation VotingSimulator

-(void)simulate{
//Select Election name
char electionNameByUser[60];
NSLog(@"Please enter election name: ");
scanf("%[^\n]", electionNameByUser);
NSString *electionName = [NSString stringWithFormat:@"%s", electionNameByUser];

//First candidate
char firstCandidateByUser[60];
NSLog(@"Enter a name for candidate 1: ");
scanf(" %[^\n]", firstCandidateByUser);
NSString *firstCandidate = [NSString stringWithFormat:@"%s", firstCandidateByUser];

//second candidate
char secondCandidateByUser[60];
NSLog(@"Enter a name for candidate 2: ");
scanf(" %[^\n]", secondCandidateByUser);
NSString *secondCandidate = [NSString stringWithFormat:@"%s", secondCandidateByUser];

//third candidate
char thirdCandidateByUser[60];
NSLog(@"Enter a name for candidate 3: ");
scanf(" %[^\n]", thirdCandidateByUser);
NSString *thirdCandidate = [NSString stringWithFormat:@"%s", thirdCandidateByUser];

//fourth candidate
char fourthCandidateByUser[60];
NSLog(@"Enter a name for candidate 4: ");
scanf(" %[^\n]", fourthCandidateByUser);
NSString *fourthCandidate = [NSString stringWithFormat:@"%s", fourthCandidateByUser];

Election *Election2015 = [[Election alloc] initWithElectionName:electionName];

Contender *Candidate1 = [[Contender alloc] initWithName:firstCandidate];
Contender *Candidate2 = [[Contender alloc] initWithName:secondCandidate];
Contender *Candidate3 = [[Contender alloc] initWithName:thirdCandidate];
Contender *Candidate4 = [[Contender alloc] initWithName:fourthCandidate];

[Election2015 addContender:Candidate1];
[Election2015 addContender:Candidate2];
[Election2015 addContender:Candidate3];
[Election2015 addContender:Candidate4];

ElectionManager *Manager = [[ElectionManager alloc] init];

[Manager manage:Election2015];
[Manager initiatePolling];
[Manager displayResults];

}

@end

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

VotingSimulator *getReadyToVote = [[VotingSimulator alloc] init];
[getReadyToVote simulate];
}
return 0;
}
}
74 changes: 72 additions & 2 deletions Person/Person/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ - (NSString *)city;
- (void)setPhoneNumber:(NSString *)phoneNumber;
- (NSString *)phoneNumber;



-(void) changePersonsName:(Person *)aPerson toName:(NSString *)newName;

- (BOOL)checkSameCity:(Person *)otherPerson;

- (Person *)registerChild;

@end

@implementation Person {
NSString *_name;
NSString *_phoneNumber;
NSString *_city;
Person *_returnChild;
}

- (void)setName:(NSString *)name {
Expand All @@ -51,13 +60,74 @@ - (NSString *)phoneNumber {
return _phoneNumber;
}


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

if ([[otherPerson city] isEqualToString: [self city]]){
NSLog (@"%@ and %@ live in the same city" , [self name], [otherPerson name]);
return YES;
} else {
NSLog (@"%@ and %@ do not live in the same city" , [self name], [otherPerson name]);

return NO;
}
}

-(void) changePersonsName:(Person *)aPerson toName:(NSString *)newName {
[aPerson setName: newName];
}


- (Person *)registerChild {
Person* child = [[Person alloc]init];

[child setPhoneNumber:_phoneNumber];
[child setCity:_city];
[child setName:@"Baby"];
return child;
}




@end


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


Person *Carl = [[Person alloc] init];
Person *Matt = [[Person alloc] init];

[Carl setName: @"Carl"];
[Carl setCity: @"Queens"];
[Carl setPhoneNumber:@"599-6452"];

[Matt setName: @"Matt"];
[Matt setCity: @"New York"];
[Matt setPhoneNumber:@"600-3454"];



[Carl checkSameCity:Matt];

NSLog(@"Sorry Carl, I don't like you anymore. I will change your name!");
[Carl setName:@"Jack"];
NSLog (@"Carl's new name is %@ ", [Carl name]);


NSLog (@"***** Matt has a babyyyy *****");
Person *mattsBaby = [Matt registerChild];

[mattsBaby setName:@"Anthony"];

NSLog(@"His name is %@", [mattsBaby name]);
NSLog (@"Matt lives in %@ and %@ lives also in %@", [Matt city], [mattsBaby name], [mattsBaby city]);
NSLog(@"Matt's phone number is: %@" , [Matt phoneNumber]);
NSLog(@"His baby's phone number is %@", [mattsBaby phoneNumber]);

}

return 0;
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ 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***


Hello, this is Diiiiiiiiiiana