hw8_fncts.cpp

/* Programmer: Jennifer Leopold Date: November 1, 2015

File: hw8_functs.cpp

Purpose: Function definitions used for encrypting a text

file.

*/

#include <iostream>

#include "hw8_functs.h"

using namespace std;

bool isVowel(const char ch)

{

short i = 0;

bool found = false;

while ((i < NUM_VOWELS) && (! found))

if (ch == VOWELS[i])

found = true;

else i++;

return(found);

}

bool wordEndsInChar(const char word[], const char ch)

{

int endOfWord = strlen(word)-1;

// If last char in word is a punctuation character,

// look at the char that comes before that

if (ispunct(word[endOfWord]))

endOfWord--;

return(word[endOfWord] == ch);

}

void strReverse(char c1[], const char c2[])

{

int len = strlen(c2);

for (int i = 0; i < len; i++)

c1[len-i-1] = c2[i];

c1[len] = '\0';

return;

}

void transformWord(char word[])

{

int len = strlen(word);

bool removedPunctuationChar = false;

char punctChar;

// If word ends in 'u' or 'y', effectively delete it

if (wordEndsInChar(word, 'u') ||

wordEndsInChar(word, 'y'))

word[0] = '\0';

// If word ends in a vowel, append 'st' to it

else if ((isVowel(word[len-1])) ||

((ispunct(word[len-1])) && (isVowel(word[len-2]))))

{

if (ispunct(word[len-1]))

{

punctChar = word[len-1];

word[len-1] = '\0'; // remove the punctuation

removedPunctuationChar = true;

}

strcat(word, "st");

if (removedPunctuationChar)

{

len = strlen(word);

word[len-1] = punctChar; // put punct char back

word[len] = '\0';

}

}

// If word is "the", replace it with "da"

else if ((strcmp(word, "the") == 0) ||

(strcmp(word, "The") == 0))

{

strcpy(word, "da");

}

// Wherever word contains "ion", replace that with "or"

else {

for (int i = 0; i <= len-3; i++)

if (((word[i] == 'i') || (word[i] == 'I')) &&

(word[i+1] == 'o') && (word[i+2] == 'n'))

{

word[i] = 'o';

word[i+1] = 'r';

if ((i+2) == (len-1))

word[len-1] = '\0';

}

}

return;

}

void getSentence(SentenceInfo &s, istream &fin)

{

const int SENTENCE_LEN = MAX_WORD_LENGTH * MAX_WORDS +

100; // extra room for spaces & punct

char sentence[SENTENCE_LEN];

char word[MAX_WORD_LENGTH];

int i, k, sentenceLength;

// Get input for sentence

fin.getline(sentence, SENTENCE_LEN, PERIOD);

if (DEBUGGING)

cout << "Sentence is: [" << sentence << "]\n";

s.numWords = 0;

if (!fin.eof())

{

// Parse input sentence into words

i = 0;

sentenceLength = strlen(sentence);

while (i < sentenceLength)

{

k = 0;

while ((sentence[i] != SPACE) && (i < strlen(sentence)))

word[k++] = sentence[i++];

word[k] = '\0'; // null-terminate the word

strcpy(s.words[s.numWords], word);

if (DEBUGGING)

cout << "Word is: ["

<< s.words[s.numWords] << "]\n";

s.numWords++;

// Advance past spaces in the sentence to get to

// beginning of next word

while (sentence[i] == SPACE)

i++;

}

}

return;

}

void insertWord(SentenceInfo &s, const int position,

const char word[])

{

s.numWords++;

// Move every word from specified position to last

// ahead one

for (int i = s.numWords-1; i > position; i--)

strcpy(s.words[i], s.words[i-1]);

strcpy(s.words[position], word);

return;

}

void transformSentence(SentenceInfo &s)

{

char temp[MAX_WORD_LENGTH];

int wordLength;

bool removedPunctuationChar;

char punctChar;

// For ever word, translate certain characters and/or

// sequences of characters

for (int i = 0; i < s.numWords; i++)

{

if (DEBUGGING)

cout << "[" << s.words[i] << "] transformed is ";

transformWord(s.words[i]);

if (DEBUGGING)

cout << "[" << s.words[i] << "]\n";

}

// Reverse the characters in every other word

for (int i = 1; i < s.numWords; i++)

{

wordLength= strlen(s.words[i]);

if ((i % 2 == 1) && (wordLength != 0))

{

removedPunctuationChar = false;

if (ispunct(s.words[i][wordLength-1]))

{

removedPunctuationChar = true;

punctChar = s.words[i][wordLength-1];

s.words[i][wordLength-1] = '\0';

}

strReverse(temp, s.words[i]);

if (removedPunctuationChar)

{

temp[wordLength-1] = punctChar;

temp[wordLength] = '\0';

}

strcpy(s.words[i], temp);

}

}

// Insert a Homerism at a random position in sentence

insertWord(s, rand() % s.numWords,

HOMERISMS[rand() % NUM_HOMERISMS]);

// Randomly either append to the beginning or end of the

// sentence a certain phrase

if ((rand() % 100) < PERCENT_CHANCE_APPEND_PHRASE_AT_END)

insertWord(s, s.numWords, ENDING_PHRASE);

else insertWord(s, 0, BEGINNING_PHRASE);

return;

}

void openInputFile(ifstream &fin, bool &inputFromKeyboard)

{

string filenameAsString;

bool success = false;

do

{

fin.clear(); // reset connection

cout << "Enter name of input file to open "

<< " or 'keyboard': ";

cin >> filenameAsString;

cin.ignore(NUM_CHARS_TO_IGNORE_UNTIL_NEWLINE, '\n');

if (filenameAsString == "keyboard")

{

success = true;

inputFromKeyboard = true;

}

else

{

fin.open(filenameAsString.c_str());

success = fin.good();

inputFromKeyboard = false;

}

} while (!success);

return;

}

ostream& operator << (ostream &out, const SentenceInfo &s)

{

for (int i = 0; i < s.numWords; i++)

{

if (strlen(s.words[i]) > 0)

out << s.words[i];

// Output a space after this word unless this is the

// last word, or this word is "empty" (i.e., it got

// "deleted"), of this is the next to the last word

// and the last word is the ENDING_PHRASE (which begins

// with a comma)

if ((i != (s.numWords - 1)) && (strlen(s.words[i]) > 0) &&

!((i == (s.numWords - 2)) &&

(strcmp(s.words[s.numWords - 1],

ENDING_PHRASE) == 0)))

out << " ";

}

out << ". ";

return(out);

}