Example Files: mAlpha.txt
mMorse.txt
/* Morse Code Converter - Morse code is a code where each letter of the
English alphabet, each digit, and various punctuation characters are
represented by a series of dots and dashes. This program asks the user
to enter a string, and then converts that string to Morse code. */
#include "Utility.h"
/* Asks the user if he or she wants to repeat the process,
returns the decision */
char tryAgain();
/* Reads in a file containing letters a-z, the numbers 0-9, and a set
of punctuation characters,
stores the contents in a char vector */
int getAlpha(vector<char> &);
/* Reads in a file containing the Morse code equivalents of letters
a-z, the numbers 0-9, and a set of punctuation characters,
stores the contents in a string vector */
int getMorse(vector<string> &);
/* Converts a sentence from English to Morse Code,
returns the sentence */
string toMorse(const string, const vector<char>, const vector<string>);
int main()
{
vector<char> alpha;
vector<string> morse;
int fOpenAlpha = 0,
fOpenMorse = 0;
char again = ' ';
string english,
morseCode;
cout << "\n\tMORSE CODE TRANSLATOR\n";
fOpenAlpha = getAlpha(alpha);
fOpenMorse = getMorse(morse);
if (fOpenAlpha != -1 && fOpenMorse != -1)
{
do
{
cout << "\n\tEnter a sentence in English, and I will translate "
"it to Morse code for you:\n\t";
getline(cin, english);
cout << "\n\tHere is your sentence in Morse code:\n\n"
<< (morseCode = toMorse(english, alpha, morse)) << "\n";
again = tryAgain();
if (again == 'N')
{
cout << "\n\tHave a nice day!\n\n";
}
} while (again != 'N');
}
pauseSystem();
return 0;
}
/* **********************************************************
Definition: tryAgain
This function asks the user if he or she wishes to try
again. This decision is returned.
********************************************************** */
char tryAgain()
{
char again = ' ';
cout << "\n\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
/* Input validation */
while (toupper(again) != 'Y' && toupper(again) != 'N')
{
cout << "\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
}
return toupper(again);
}
/* **********************************************************
Definition: getAlpha
This function reads in a file called 'mAlpha.txt', which
contains the letters a-z, numbers 1 through 9, and a set
of punctuation characters. The contents of this file is
stored in a string vector. If the file cannot be openend,
the user is informed by a message, and the function will
exit with code -1.
********************************************************** */
int getAlpha(vector<char> &alpha)
{
ifstream alphabet;
char mAlpha;
alphabet.open("mAlpha.txt");
if (alphabet && !alphabet.eof())
{
while (alphabet >> mAlpha)
{
alpha.push_back(mAlpha);
}
}
else
{
cout << "\n\tFile open error: The file 'mAlpha.txt' could not be\n"
<< "\topened or processed. Make sure that the filename is\n"
<< "\tcorrect and the file is not damaged or has been moved\n"
<< "\tfrom the program folder.\n\n"
<< "\tPress enter to exit this program ...";
return -1;
}
alphabet.close();
return 0;
}
/* **********************************************************
Definition: getMorse
This function reads in a file called 'mMorse.txt', which
contains the Morse code equivalents of the letters a-z,
numbers 1 through 9, and a set of punctuation characters.
The contents of this file is stored in a string vector.
If the file cannot be openend, the user is informed by a
message, and the function will exit with code -1.
********************************************************** */
int getMorse(vector<string> &morse)
{
ifstream morseAlpha;
string mMorse;
morseAlpha.open("mMorse.txt");
if (morseAlpha && !morseAlpha.eof())
{
while (getline(morseAlpha, mMorse))
{
morse.push_back(mMorse);
}
}
else
{
cout << "\n\tFile open error: The file 'mMorse.txt' could not be\n"
<< "\topened or processed. Make sure that the filename is\n"
<< "\tcorrect and the file is not damaged or has been moved\n"
<< "\tfrom the program folder.\n\n"
<< "\tPress enter to exit this program ...";
return -1;
}
morseAlpha.close();
return 0;
}
/* **********************************************************
Definition: toMorse
This function accepts a string object and two vectors as
arguments. It translates a sentence into Morse code. The
string object containing the translation is returned.
********************************************************** */
string toMorse(const string input, const vector<char> alpha,
const vector<string> morse)
{
unsigned int startScan = 0,
index = 0;
string morseCode;
for (startScan = index; startScan < input.length(); startScan++)
{
for (index = 0; index < morse.size(); index++)
{
if (tolower(input.at(startScan)) == alpha.at(index))
{
morseCode.append(morse.at(index) + " ");
}
}
if (isspace(input.at(startScan)))
{
morseCode.insert(morseCode.length(), " ");
}
}
return morseCode;
}
Example Output:
/* Pig Latin - This program reads a sentence as input and converts each
word to "Pig Latin." To convert a word to Pig Latin, the first letter
is removed and placed at the end of the word, then the string "ay" is
appended to the word. For example:
* English: I SLEPT MOST OF THE NIGHT
* Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY */
#include "Utility.h"
/* Asks the user if he or she wants to repeat the process,
returns the decision */
char tryAgain();
/* Converts each word to pig latin,
returns the words */
string pigLatin(string);
/* Converts each word to uppercase */
void wordCapitalizer(string &);
/* Removes punctuation from a sentence */
void removePunct(string &);
/* Clears the input of the three string object */
void clearInput(string &, string &, string &);
int main()
{
char again = ' ';
string word;
string pigSentence;
string englishSentence;
cout << "\tPIG LATIN TRANSLATOR\n\n"
<< "\tEnter a sentence, and I will translate it for you "
<< "from English to Pig Latin:\n\t";
do
{
while (cin >> word)
{
englishSentence.append(word + " ");
removePunct(word);
wordCapitalizer(word);
pigSentence.append(pigLatin(word));
if (cin.get() == '\n')
{
cout << "\n\tThis was your original sentence:\n\t"
<< englishSentence << "\n";
cout << "\n\tThis is your sentence in Pig Latin:\n\t"
<< pigSentence;
break;
}
}
again = tryAgain();
if (again == 'Y')
{
cout << "\n\tPlease enter another sentence:\n\t";
clearInput(word, pigSentence, englishSentence);
}
else
{
cout << "\tHave a nice day!\n\n";
}
} while (again == 'Y');
pauseSystem();
return 0;
}
/* **********************************************************
Definition: pigLatin
This function stores the first letter of each word in a
variable, then the first letter is erased, and in a final
step the letter plus "AY " is appended to the word.
********************************************************** */
string pigLatin(string word)
{
string tmp = word.substr(0, 1);
string pLatin = "AY ";
word.erase(word.begin());
word.append(tmp + pLatin);
return word;
}
/* **********************************************************
Definition: removePunct
This function erases all punctuation from the string.
********************************************************** */
void removePunct(string &sentence)
{
for (unsigned int i = 0; i < sentence.length(); i++)
{
if (ispunct(sentence.at(i)))
{
sentence.erase(i++, 1);
}
}
}
/* **********************************************************
Definition: wordCapitalizer
This function converts a word to uppercase.
********************************************************** */
void wordCapitalizer(string &word)
{
for (unsigned int index = 0; index < word.length(); index++)
{
if (isalpha(word.at(index)))
{
word.at(index) = toupper(word.at(index));
}
}
}
/* **********************************************************
Definition: tryAgain
This function asks the user if he or she wishes to try
again. This decision is returned.
********************************************************** */
char tryAgain()
{
char again = ' ';
cout << "\n\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
/* Input validation */
while (toupper(again) != 'Y' && toupper(again) != 'N')
{
cout << "\n\tDo you wish to try this again? ";
cin >> again;
}
return toupper(again);
}
/* **********************************************************
Definition: clearInput
This function clears the contents in the string objects.
********************************************************** */
void clearInput(string &word, string &pigSentence,
string &englishSentence)
{
word.clear();
pigSentence.clear();
englishSentence.clear();
}
Example Output:
Example File: text.txt
/* Character Analysis - This program reads the contents of a file called
text.txt and determines the following:
* The number of uppercase letters in the file
* The number of lowercase letters in the file
* The number of digits in the file */
#include "Utility.h"
/* Reads in the contents of the file 'text.txt',
stores the contents in a string object */
int getText(string &);
/* Displays the text stored in the string object */
void displayText(const string);
/* Examines the string object, counts every occurence of
uppercase and lowercase characters as well as digits,
outputs the result */
void analyzeText(const string);
int main()
{
int fRead = 0;
string fullText;
fRead = getText(fullText);
if (fRead == 0)
{
displayText(fullText);
analyzeText(fullText);
}
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getText
This function opens and reads in the contents of a file
called 'text.txt.' The content is read in and stored into
a string object.
********************************************************** */
int getText(string &fullText)
{
ifstream textFile;
string tmp;
textFile.open("txt.txt");
if (textFile)
{
while (getline(textFile, tmp) && !textFile == '\0')
{
fullText.insert(fullText.length(), "\t" + tmp + "\n");
}
}
else
{
cout << "\n\tFile open error: The file 'text.txt' could not be\n"
<< "\topened or processed successfully. Make sure that the\n"
<< "\tfilename is correct and the file is not damaged or has\n"
<< "\tbeen moved from the program folder.\n\n"
<< "\tPress enter to exit this program ...";
return -1;
}
textFile.close();
return 0;
}
/* **********************************************************
Definition: displayText
This function accepts a string object as argument. It
ouputs the contents to screen.
********************************************************** */
void displayText(const string fullText)
{
for (char i : fullText)
{
cout << i;
}
}
/* **********************************************************
Definition: analyzeText
This function accepts a string object as argument. It
finds and counts all upper- and lowercase characters, as
well as digits. The result is output to screen.
********************************************************** */
void analyzeText(const string fullText)
{
int lower = 0,
upper = 0,
digit = 0;
for (char i : fullText)
{
islower(i) ? lower++ : i;
isupper(i) ? upper++ : i;
isdigit(i) ? digit++ : i;
}
cout << "\n\tThis text contains " << setw(4) << digit
<< " digits.";
cout << "\n\tThis text contains " << setw(4) << upper
<< " uppercase characters.\n";
cout << "\tThis text contains " << lower
<< " lowercase characters.\n";
}
Example Output:
/* Word Separator - This program accepts as input a sentence in which
all words are run together, but the first character of each word is
uppercase. The sentence is converted to a string in which the words
are separated by spaces and only the first word starts with an
uppercase letter. For example the string "StopAndSmellTheRose."
Would be converted to "Stop and smell the roses." */
#include "Utility.h"
/* Introduces the functionality of this program to the user */
void intro();
/* Asks the user if he or she wants to repeat the process,
returns the decision */
char tryAgain();
/* Searches for uppercase characters in a sentence,
inserts spaces to separate the words at that position */
void separator(string &);
/* Replaces uppercase characters in a sentence with
their lowercase equivalents */
void senDecapitalizer(string &);
/* Replaces lowercase characters at the beginning of sentences
with their uppercase equivalent */
void senCapitalizer(string &);
int main()
{
int firstRun = 1;
char again = ' ';
string sentence = " ";
do
{
if (firstRun)
{
intro();
firstRun = 0;
}
else
{
cout << "\n\n\tWORD SEPARATOR\n\n";
}
cout << "\tEnter a sentence:\n\t";
getline(cin, sentence);
separator(sentence);
senDecapitalizer(sentence);
senCapitalizer(sentence);
cout << "\n\tThis is your sentence, correctly formatted:\n\t";
cout << sentence;
again = tryAgain();
if (again == 'N')
{
cout << "\n\tHave a nice day!\n\n";
}
} while (again == 'Y');
pauseSystem();
return 0;
}
/* **********************************************************
Definition: intro
This function introduces the basic functionality of this
program to the user.
********************************************************** */
void intro()
{
cout << "\n\tWORD SEPARATOR\n\n"
<< "\tEnter a sentence without spaces, each word starting with\n"
<< "\tan uppercase letter, and I will separate the words.\n"
<< "\tA sentence such as:\n\n"
<< "\t'ThisSentenceDoesNotContainAnySpaces'\n\n"
<< "\twill be formatted, to look like this:\n\n"
<< "\t'This Sentence Does Not Contain Any Spaces.'\n\n"
<< "\tYou can repeat this process as often as you like to,\n"
<< "\tas long as you enter 'y' when asked.\n\n";
}
/* **********************************************************
Definition: tryAgain
This function asks the user if he or she wishes to try
again. This decision is returned.
********************************************************** */
char tryAgain()
{
char again = ' ';
cout << "\n\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
/* Input validation */
while (toupper(again) != 'Y' && toupper(again) != 'N')
{
cout << "\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
}
return toupper(again);
}
/* **********************************************************
Definition: separator
This function searches for the occurence of uppercase
characters in a sentence. If found, a space is inserted to
separate the characters.
********************************************************** */
void separator(string &sentence)
{
for (unsigned int index = 1; index < sentence.length(); index++)
{
if (isupper(sentence[index]))
{
sentence.insert((index++), " ");
}
}
}
/* **********************************************************
Definition: senDecapitalizer
This function replaces all uppercase characters found in
a sentence by their lowercase equivalent.
********************************************************** */
void senDecapitalizer(string &sentence)
{
for (unsigned int index = 1; index < sentence.length(); index++)
{
if (!isupper(sentence[index]))
{
sentence[index+1] = tolower(sentence[index+1]);
}
}
}
/* **********************************************************
Definition: senCapitalizer
This function converts letters at the beginning of each
sentence to their uppercase equivalents.
********************************************************** */
void senCapitalizer(string &sentence)
{
bool isDelim = false;
for (unsigned int index = 0; index < sentence.length(); index++)
{
if (sentence.at(index) == '.' || sentence.at(index) == '!' ||
sentence.at(index) == '?')
{
isDelim = false;
}
if (isalpha(sentence.at(index)) && isDelim == false)
{
isDelim = true;
sentence.at(index) = toupper(sentence.at(index));
}
}
}
Example Output:
/* Date Printer - This program reads a string from the user containing a
date in the form mm/dd/yyyy. It prints the date in the form March 12,
2014. */
#include "Utility.h"
/* Performs various checks to confirm validity of input,
returns the result */
bool checkDate(string);
/* Determines the correct month name,
returns the month name */
string months(int);
/* Inserts month, day and year into the date string,
displays the date */
void printDate(string, int, int);
int main()
{
int month = 0,
day = 0,
year = 0;
string date = " ";
cout << "\n\tDATE PRINTER\n\n"
<< "\tEnter a date in the format 'mm/dd/yyyy' and I will\n"
<< "\tconvert and display it. For example, after entering\n"
<< "\t'01/12/2017', the output will be 'January 12, 2017.'\n\n"
<< "\tPlease enter a date: ";
getline(cin, date);
while (checkDate(date) == false)
{
if (checkDate(date) == false)
{
cout << "\n\tThe date entered is invalid.\n"
<< "\tPlease enter a date: ";
getline(cin, date);
}
}
month = stoi(date.substr(0));
day = stoi(date.substr(3));
year = stoi(date.substr(6));
date = months(month);
printDate(date, day, year);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: checkDate
This function performs the following checks on the string
* Correct length of the date string (This is the only
condition that immediately returns the result in case
the validity check fails)
* Existence of delimiting characters '/'
* Correctness of month, day, year input
* Determins whether the year is a leap year. Based on
the result, the month and day input is validated once
more.
The result is returned.
********************************************************** */
bool checkDate(string date)
{
bool validDate = true;
bool isLeapYear = false;
int month = 0,
day = 0,
year = 0;
char delimiter = '/';
if (date.length() < 10)
{
return validDate = false;
}
if (date.at(2) != delimiter || date.at(5) != delimiter)
{
validDate = false;
}
month = stoi(date.substr(0));
if (month < 1 || month > 12)
{
validDate = false;
}
day = stoi(date.substr(3));
if (day < 1 || day > 31)
{
validDate = false;
}
year = stoi(date.substr(6));
if (year < 1 || year > 2100)
{
validDate = false;
}
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
isLeapYear = true;
}
if (isLeapYear && month == 2 && day > 29)
{
validDate = false;
}
if (isLeapYear == false && month == 2 && day > 28)
{
validDate = false;
}
return validDate;
}
/* **********************************************************
Definition: months
This function determines the name of a month, based on the
integer value passed to it. The name is returned.
********************************************************** */
string months(const int month)
{
const string monthNames[12] = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
return monthNames[month-1];
}
/* **********************************************************
Definition: printDate
This function accepts a string and two int values as
arguments. It inserts day and year into the date string,
and displays the date.
********************************************************** */
void printDate(string date, int day, int year)
{
date.insert(date.length(), " " + to_string(day) + ", " + to_string(year));
cout << "\n\tYour date is: " << date << endl;
}
Example Output:
/* Password Verifier - Imagine we are developing a software package that
requires users to enter their own passwords. Our software requires that
user's passwords meet the following criteria:
* The password should be at least six characters long
* The password should contain at least one uppercase
and at least one lowercase letter
* The password should have at least one digit
This program asks for a password and then verifies that it meets the
stated criteria. If it doesn't, the program displays a message telling
the user why. */
#include "Utility.h"
/* Determines the length of password,
returns the result */
bool checkLength(string);
/* Determines whether the password contains uppercase characters,
returns the result */
bool containsUpper(string);
/* Determines whether the password contains lowercase characters,
returns the result */
bool containsLower(string);
/* Determines whether the password contains digits,
returns the result */
bool containsDigit(string);
int main()
{
int numTries = 5;
string passphrase = "moRioka9713";
string password = " ";
cout << "\n\tKaibun Game - Login\n"
<< "\t-------------------\n";
do
{
cout << "\n\tPlease enter your password: ";
getline(cin, password);
if (password != passphrase)
{
if (checkLength(password) == false)
{
cout << "\n\tPassword length mismatch.\n";
}
if (containsUpper(password) == false)
{
cout << "\tPassword does not contain uppercase characters.\n";
}
if (containsLower(password) == false)
{
cout << "\tPassword does not contain lowercase characters.\n";
}
if (containsDigit(password) == false)
{
cout << "\tPassword does not contain digits.\n";
}
cout << "\n\tYou have " << (numTries -= 1) << " tries left\n";
if (numTries == 0)
{
cout << "\n\tYour password could not be verified.\n"
<< "\tNow closing this program ...";
}
}
else if (password == passphrase)
{
cout << "\n\tYou have been verified successfully.\n";
}
} while (numTries > 0 && password != passphrase);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: checkLength
This function verifies that the password is at least six
characters long. The result is returned.
********************************************************** */
bool checkLength(string password)
{
bool length = false;
password.length() >= 6 ? length = true : length = false;
return length;
}
/* **********************************************************
Definition: containsUpper
This function verifies that the password contains at least
one uppercase character. The result is returned.
********************************************************** */
bool containsUpper(string password)
{
bool isUpper = false;
for (size_t i = 0; i < password.length(); i++)
{
if (isupper(password[i]))
{
isUpper = true;
}
}
return isUpper;
}
/* **********************************************************
Definition: containsLower
This function verifies that the password contains at least
one lowercase character. The result is returned.
********************************************************** */
bool containsLower(string password)
{
bool isLower = false;
for (size_t i = 0; i < password.length(); i++)
{
if (islower(password[i]))
{
isLower = true;
}
}
return isLower;
}
/* **********************************************************
Definition: containsDigit
This function determines whether the password contains any
digits. The result is returned.
********************************************************** */
bool containsDigit(string password)
{
bool isDigit = false;
for (size_t i = 0; i < password.length(); i++)
{
if (isdigit(password[i]))
{
isDigit = true;
}
}
return isDigit;
}
Example Output:
/* Case Manipulator - This program contains three functions, upper, lower,
and reverse.
* The upper function accepts a pointer to a C-string as an argument.
It steps through each character in the string, converting it to
uppercase.
* The lower function, too, accepts a pointer to a C-string as an
argument. It steps through each character in the string, converting
it to lowercase.
* Like upper and lower, reverse accepts a pointer to a C-string. As it
steps through the string, it tests each character to determine
whether it is upper- or lowercase. If a character is uppercase, it
is converted to lowercase. Likewise, if a character is lowercase,
it is converted to uppercase.
The functions are tested by asking for a string in function main, then
passing it to them in the order: reverse, lower, and upper. */
#include "Utility.h"
/* Determines character casing, changing upper- to lowercase letters
and vice versa */
void reverse(char *);
/* Converts all characters from upper- to lowercase */
void lowercase(char *);
/* Converts all characters from lower- to uppercase */
void uppercase(char *);
int main()
{
const int MAX_CHARS = 501;
char sentence[MAX_CHARS];
cout << "\n\tCASE MANIPULATOR\n\n\t"
<< "Enter a sentence, " << (MAX_CHARS - 1)
<< " characters in length, and I will\n\t"
<< "first reverse all upper and lowercase characters, then\n\t"
<< "I will change all characters to lowercase, and lastly I\n\t"
<< "will change all characters to uppercase.\n\n";
cout << "\n\tPlease enter your sentence:\n\t";
cin.getline(sentence, MAX_CHARS);
cout << "\n\n\tCHARACTER CASE REVERSAL\n";
reverse(sentence);
cout << "\n\tAll character cases have been reversed:\n"
<< "\t" << sentence << "\n\n";
cout << "\n\tCHARACTERS TO LOWERCASE:\n";
lowercase(sentence);
cout << "\n\tAll uppercase characters have been changed "
<< "to lowercase:\n"
<< "\t" << sentence << "\n\n";
cout << "\n\tCHARACTERS TO UPPERCASE:\n";
uppercase(sentence);
cout << "\n\tAll lowercase characters have been changed "
<< "to uppercase:\n"
<< "\t" << sentence << "\n\n";
pauseSystem();
return 0;
}
/* **********************************************************
Definition: reverse
This function accepts a pointer to a C-string as argument.
It reverses all upercase characters to lowercase and vice
versa.
********************************************************** */
void reverse(char *senPtr)
{
for (size_t index = 0; senPtr[index] != '\0'; index++)
{
if (isupper(senPtr[index]))
{
senPtr[index] = tolower(senPtr[index]);
}
else if (islower(senPtr[index]))
{
senPtr[index] = toupper(senPtr[index]);
}
}
}
/* **********************************************************
Definition: lowercase
This function accepts a pointer to a C-string as argument.
It converts all characters from upper- to lowercase.
********************************************************** */
void lowercase(char *senPtr)
{
for (size_t index = 0; senPtr[index] != '\0'; index++)
{
if (isupper(senPtr[index]))
{
senPtr[index] = tolower(senPtr[index]);
}
}
}
/* **********************************************************
Definition: uppercase
This function accepts a pointer to a C-string as argument.
It converts all characters from lower- to uppercase.
********************************************************** */
void uppercase(char *senPtr)
{
for (size_t index = 0; senPtr[index] != '\0'; index++)
{
if (islower(senPtr[index]))
{
senPtr[index] = toupper(senPtr[index]);
}
}
}
Example Output: