1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
enum SpecialCharacter { Exclamation = 1 , Comma, Period , QuestionMark , Semicolon , Colon };
// Function Prototypes: Reference the definition heading for the description of each function
bool isVowel ( char ch );
std::string rotate ( std::string pStr );
std::string pigLatinString ( std::string pStr );
int getSpecialCharacter ( std::string pStr );
void addBackSpecialCharacter ( std::string& pStr , SpecialCharacter specialCharacter );
int main()
{
std::string str;
std::string sentence;
std::string finalSentence;
std::cout << "Enter a String: ";
std::getline ( std::cin , sentence );
std::istringstream strStream ( sentence );
while ( strStream >> str ) {
str = pigLatinString ( str );
finalSentence += ( str + ' ' );
}
std::cout << std::endl;
std::cout << "The pig Latin form of \"" << sentence << "\" is: \"" << finalSentence << "\b\"" <<std::endl;
return 0;
}
/****************************************************************
This function takes a character as a parameter and returns true
if the character is a vowel and false otherwise.
****************************************************************/
bool isVowel ( char ch ) {
ch = toupper ( ch ); // Removes case-sensitivity of user input
int counter = 0; // Decides when to increment the ASCII value by 4 or 6
int vowelPosition = 65; // Initialize to the ASCII value of the first vowel, 'A'
while ( vowelPosition <= 85 ) { // 85 is the ASCII value of the last vowel, 'U'
if ( ch == vowelPosition )
return true;
else if ( counter < 2 ) {
vowelPosition += 4;
counter ++;
}
else
vowelPosition += 6;
}
// Normally "Y" is not a vowel, but for this program, "Y" is assumed to be a vowel
if ( ch == 'Y' )
return true;
return false;
}
/****************************************************************
This function takes a string as a parameter, removes the first
character of the string, and places it at the end of the string.
This is done by extracting the substring, starting at position 1
(which is the second character) until the end of the string, and
then adding the first character of the string. The new string is
returned as the value of this function.
****************************************************************/
std::string rotate ( std::string pStr ) {
return ( pStr.substr ( 1 ) + pStr [ 0 ] );
}
/****************************************************************
This function takes a string, pStr, as a parameter and returns
the pig Latin form of pStr. Suppose pStr denotes the string to
be converted to its pig Latin form. There are three possible
cases: pStr [0] is a vowel, pStr contains a vowel and the first
character of pStr is not a vowel, or pStr contains no vowels.
Suppose that pStr [0] is not a vowel. Move the first character of
pStr to the end of pStr. This process is repeated until either
the first character of pStr has become a vowel or all the
characters of pStr are checked, in which case pStr does not
contain any vowels.
****************************************************************/
std::string pigLatinString ( std::string pStr ) {
bool foundVowel = false;
int hasSpecialCharacter = getSpecialCharacter( pStr );
int len;
SpecialCharacter specialCharacter; // ENUM Special Character variable
// Convert the int value of hasSpecialCharacter to corresponding ENUM value
specialCharacter = static_cast < SpecialCharacter > ( hasSpecialCharacter );
// If the pStr is just a special character, return that special character
if ( hasSpecialCharacter && pStr.length () == 1 ) {
switch ( specialCharacter ) {
case Exclamation:
return "!";
case Comma:
return ",";
case Period:
return ".";
case QuestionMark:
return "?";
case Semicolon:
return ";";
case Colon:
return ":";
}
}
if ( hasSpecialCharacter )
pStr.pop_back ();
if ( isVowel ( pStr [ 0 ] ) )
pStr = pStr + "-way";
else {
pStr = pStr + '-';
pStr = rotate ( pStr );
len = pStr.length ();
for ( int iter = 1 ; iter < len - 1 ; iter ++ ) {
if ( isVowel ( pStr [ 0 ] ) ) {
foundVowel = true;
break;
}
else
pStr = rotate ( pStr );
}
if ( !foundVowel )
pStr = pStr.substr ( 1 , len ) + "-way";
else
pStr = pStr + "ay";
}
if ( hasSpecialCharacter )
addBackSpecialCharacter ( pStr , specialCharacter );
return pStr;
}
/****************************************************************
This function takes String as a reference and a Special Character
enum variable as parameters. It adds back the special character
at the end of the string.
****************************************************************/
void addBackSpecialCharacter ( std::string& pStr , SpecialCharacter specialCharacter ) {
switch ( specialCharacter ) {
case Exclamation:
pStr = pStr + '!';
break;
case Comma:
pStr = pStr + ',';
break;
case Period:
pStr = pStr + '.';
break;
case QuestionMark:
pStr = pStr + '?';
break;
case Semicolon:
pStr = pStr + ';';
break;
case Colon:
pStr = pStr + ':';
break;
}
}
/****************************************************************
This function takes a String as a parameter. It looks at the last
element of the String and if it is a special character, it
returns an integer based on what the special character is.
It returns 0 (false) if there is no special character at the end.
Reference ENUM, SpecialCharacter at the top of the program
for information on which integer corresponds to which special
character.
****************************************************************/
int getSpecialCharacter ( std::string pStr ) {
if ( pStr [ pStr.length () - 1 ] == '!' )
return 1;
if ( pStr [ pStr.length () - 1 ] == ',' )
return 2;
if ( pStr [ pStr.length () - 1 ] == '.' )
return 3;
if ( pStr [ pStr.length () - 1 ] == '?' )
return 4;
if ( pStr [ pStr.length () - 1 ] == ';' )
return 5;
if ( pStr [ pStr.length () - 1 ] == ':' )
return 6;
return 0;
}
|