Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having issues with this question , it is computer programming class and we are using C + + programming. can you correct this

I am having issues with this question , it is computer programming class and we are using C++ programming. can you correct this program? Use C++ programming to redo program
Use C++ programming , write a program:
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.
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-06-2119:03:43.074440
Status: FAILED!
Check: 3
Test: File test 2
Reason: Unable to find 'o-tay end-spay e-thay irst-fay ew-fay eeks-way' in the program.
Error : str - AssertionError
Timestamp: 2024-06-2119:03:43.074565
#include
#include
#include
#include
#include
#include
bool isPunctuation(char c){
return (c ==','|| c =='.'|| c =='?'|| c ==';'|| c ==':');
}
std::string pigLatin(const std::string& word){
std::string result ="";
std::string::size_type len = word.length();
std::string::size_type first_vowel = len; // position of the first vowel
// Find the position of the first vowel
for (std::string::size_type i =0; i < len; ++i){
if (std::tolower(word[i])=='a'|| std::tolower(word[i])=='e'||
std::tolower(word[i])=='i'|| std::tolower(word[i])=='o'||
std::tolower(word[i])=='u'){
first_vowel = i;
break;
}
}
// Construct the Pig Latin word
if (first_vowel ==0){
// If the word starts with a vowel
result = word +"-Hay";
} else {
// If the word starts with consonants
result = word.substr(first_vowel)+"-"+ word.substr(0, first_vowel)+"ay";
}
return result;
}
int main(){
std::ifstream inFile("Ch7_Ex3In.txt");
std::ofstream outFile("Ch7_Ex3Out.txt");
if (!inFile){
std::cerr << "Error opening input file." << std::endl;
return 1;
}
if (!outFile){
std::cerr << "Error creating output file." << std::endl;
return 1;
}
std::string line;
while (std::getline(inFile, line)){
std::istringstream iss(line);
std::string word;
bool firstWord = true;
while (iss >> word){
// Check if the word ends with a punctuation mark
char lastChar = word.back();
bool hasPunctuation = isPunctuation(lastChar);
// Remove punctuation if present
if (hasPunctuation){
word.pop_back();
}
// Convert to Pig Latin
std::string pigLatinWord = pigLatin(word);
// Append punctuation back if present
if (hasPunctuation){
pigLatinWord += lastChar;
}
// Write the word to the output file
if (!firstWord){
outFile <<"";
}
outFile << pigLatinWord;
firstWord = false;
}
outFile << std::endl; // End of line
}
std::cout << "Pig Latin conversion completed. Output saved in Ch7_Ex3Out.txt."<< std::endl;
inFile.close();
outFile.close();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

=+b) Find the standard deviations.

Answered: 1 week ago