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 ello-Hay!. Assume that the text contains the following punctuation marks:
,(comma),
.(period),
?(question mark),
; (semicolon),
: (colon).
Store the output in Ch7_Ex3Out.txt.
Task #01: Program Outputs Correctly
Test Feedback:
Status: PASSED! Check: 1 Test: Run program Reason: None Timestamp: 2024-11-0802:21:12.785961
Status: FAILED! Check: 2 Test: File test 1 Reason: Unable to find 'e-thay Internet-way. You-way an-cay atch-way' in the program. Error : str - AssertionError Timestamp: 2024-11-0802:21:12.786367
Status: PASSED! Check: 3 Test: File test 2 Reason: o-tay end-spay e-thay irst-fay ew-fay eeks-way +` was found in the program.None Timestamp: 2024-11-0802:21:12.786686
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 toPigLatin(const string& word){
string processedWord = word;
char punctuation ='\0';
// Handle punctuation at the end
if (!processedWord.empty() && ispunct(processedWord.back())){
punctuation = processedWord.back();
processedWord = processedWord.substr(0, processedWord.length()-1); // Remove punctuation
}
if (!processedWord.empty() && isalpha(processedWord[0])){
bool isCapitalized = isupper(processedWord[0]);
string pigLatin;
// Check if the first letter is a vowel
if (string("aeiouAEIOU").find(processedWord[0])!= string::npos){
pigLatin = processedWord +"-way";
} else {
// Word starts with a consonant
size_t firstVowelIndex = processedWord.find_first_of("aeiouAEIOU");
if (firstVowelIndex != string::npos){
// Consonant cluster
pigLatin = processedWord.substr(firstVowelIndex)+"-"+ processedWord.substr(0, firstVowelIndex)+"ay";
} else {
// If no vowels, treat the whole word as consonant
pigLatin = processedWord +"-ay";
}
}
// Adjust capitalization
if (isCapitalized){
pigLatin[0]= toupper(pigLatin[0]);
for (size_t i =1; i < pigLatin.length(); i++){
pigLatin[i]= tolower(pigLatin[i]);
}
}
// Reattach punctuation if present
if (punctuation !='\0'){
pigLatin += punctuation;
}
return pigLatin;
}
return processedWord; // Return the original word if it's not valid
}
// Tokenize text into words and punctuation
vector tokenize(const string& text){
vector tokens;
regex wordRegex(R"(\w+(-\w+)?|[.,?!;:'"-])"); // Matches words, hyphenated words, or punctuation
auto wordsBegin = sregex_iterator(text.begin(), text.end(), wordRegex);
auto wordsEnd = sregex_iterator();
for (auto it = wordsBegin; it != wordsEnd; ++it){
tokens.push_back(it->str());
}
return tokens;
}
// Process text into Pig Latin for the entire sentence
string processTextToPigLatin(const string& text){
vector words = tokenize(text);
string result;
for (const string& word : words){
result += toPigLatin(word)+"";
}
if (!result.empty()){
result.pop_back(); // Remove the trailing space
}
return result;
}
int main(){
ifstream inFile("Ch7_Ex3In.txt");
ofstream outFile("Ch7_Ex3Out.txt");
if (!inFile){
cerr << "Error: Input file not found." << endl;
return 1;
}
if (!outFile){
cerr << "Error: Cannot open output file." << endl;
return 1;
}
string line;
while (getline(inFile, line)){
string pigLatinLine = processTextToPigLatin(line);
outFile << pigLatinLine <<'
'; // Preserve exact line structure
}
cout << "Pig Latin conversion complete. Check Ch7_Ex3Out.txt."<< endl;
inFile.close();
outFile.close();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!