Skip to content
Open
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
104 changes: 104 additions & 0 deletions E-Memory-Pointers/code/pig_latin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *convert(char *word, char *pig_word) {

/*
* Copy second half of word to first half of pig_word
*
*/

int half_word_len = strlen(word)/2;

strcpy(pig_word, word + half_word_len);

/*
* Copy first half of word to second half of pig_word
*
*/


int idx = 0;
int start_idx = strlen(pig_word);

while(idx < half_word_len) {

pig_word[start_idx + idx] = *(word + idx);
idx++;
}


pig_word[start_idx + idx] = 0;

/*
* Append "ay" to end of pig_word
*
*/

strcat(pig_word, "ay");

return pig_word;
}

int main(int argc, char **argv) {

/*
* Allocate space and evaluate phrase_len using sizeof()
*
*/

char *pig_phrase[argc];
pig_phrase[argc - 1] = NULL;

int phrase_len = sizeof(pig_phrase) / sizeof(char *);


/*
* iterate through phrase
* we will allocate space for each word
* then pass that space to be filled by a convert function
*
*/

for (int i = 1; i < argc; i++) {

char *word = *(argv+i);

/*
* Allocate space for new word
*
*/

int pig_word_len = strlen(word) + strlen("ay") + 1;


// Program wont work unless we malloc, reveal later
//char pig_word[pig_word_len];
char *pig_word = malloc(pig_word_len * sizeof(char));

/*
* Fill in array with converted pointer
*
*/

pig_phrase[i-1] = convert(word, pig_word);

}

/*
* Iterate through and print new phrase
*
*/
char **pig_phrase_itr = pig_phrase;
/*
* Question: Why do we need a char** here instead of using pig_phrase itself?
*
*/
while (*pig_phrase_itr){
printf("%s ", *pig_phrase_itr);
free(*pig_phrase_itr);
pig_phrase_itr++;
}
printf("\n");
}