Question: The Programming Example: Pig Latin Strings converts a string into the Pig Latin form, but it processes only one word. Rewrite the program so that
The Programming Example: Pig Latin Strings converts a string into the Pig Latin form, but it processes only one word. Rewrite the program so that it can be used to process a text of an unspecified length. If a word ends with a punctuation mark, in the pig Latin form, put the punctuation at the end of the string. For example, the pig Latin form of Hello! is elloHay!. Assume that the text contains the following punctuation marks:
comma
period
question mark
; semicolon
: colon
Store the output in ChExOut.txt
Task #: Program Outputs Correctly
Test Feedback:
Status: PASSED! Check: Test: Run program Reason: None Timestamp: ::
Status: FAILED! Check: Test: File test Reason: Unable to find ethay Internetway. Youway ancay atchway' in the program. Error : str AssertionError Timestamp: ::
Status: PASSED! Check: Test: File test Reason: otay endspay ethay irstfay ewfay eeksway was found in the program.None Timestamp: ::
Having problem resolving this issue in c using this code and need help clearing it
#include
#include
#include
#include
#include
#include
#include
using namespace std;
Convert a single word to Pig Latin
string toPigLatinconst string& word
string processedWord word;
char punctuation ;
Handle punctuation at the end
if processedWord.empty && ispunctprocessedWordback
punctuation processedWord.back;
processedWord processedWord.substr processedWord.length; Remove punctuation
if processedWord.empty && isalphaprocessedWord
bool isCapitalized isupperprocessedWord;
string pigLatin;
Check if the first letter is a vowel
if stringaeiouAEIOUfindprocessedWord string::npos
pigLatin processedWord way";
else
Word starts with a consonant
sizet firstVowelIndex processedWord.findfirstofaeiouAEIOU;
if firstVowelIndex string::npos
Consonant cluster
pigLatin processedWord.substrfirstVowelIndex processedWord.substr firstVowelIndexay;
else
If no vowels, treat the whole word as consonant
pigLatin processedWord ay;
Adjust capitalization
if isCapitalized
pigLatin toupperpigLatin;
for sizet i ; i pigLatin.length; i
pigLatini tolowerpigLatini;
Reattach punctuation if present
if punctuation
pigLatin punctuation;
return pigLatin;
return processedWord; Return the original word if it's not valid
Tokenize text into words and punctuation
vector tokenizeconst string& text
vector tokens;
regex wordRegexRww;:; Matches words, hyphenated words, or punctuation
auto wordsBegin sregexiteratortextbegin text.end wordRegex;
auto wordsEnd sregexiterator;
for auto it wordsBegin; it wordsEnd; it
tokens.pushbackitstr;
return tokens;
Process text into Pig Latin for the entire sentence
string processTextToPigLatinconst string& text
vector words tokenizetext;
string result;
for const string& word : words
result toPigLatinword;
if result.empty
result.popback; Remove the trailing space
return result;
int main
ifstream inFileChExIntxt;
ofstream outFileChExOut.txt;
if inFile
cerr "Error: Input file not found." endl;
return ;
if outFile
cerr "Error: Cannot open output file." endl;
return ;
string line;
while getlineinFile line
string pigLatinLine processTextToPigLatinline;
outFile pigLatinLine
; Preserve exact line structure
cout "Pig Latin conversion complete. Check ChExOut.txt endl;
inFile.close;
outFile.close;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
