Answer to Question #44906 in C++ for shaheen

Question #44906
Write a c++ program to read a paragraph from a data file(.exe) and find two consecutive spaces(" ") and convert it single space.
1
Expert's answer
2014-08-21T03:39:39-0400
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main () {
// Uncomment to createdata.exe file
// ofstreamofs("data.exe", ios_base::binary | ios_base::out);

// if(ofs.is_open()) {
// ofs << "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
// ofs.close();
// }

stringparagraph; // paragraph from file
stringnewParagraph = ""; // new paragraph

// Reads string fromdata file
ifstream ifs("data.exe", ios_base::binary | ios_base::in);

if (ifs.is_open()) {
getline(ifs, paragraph);
ifs.close();
}

cout << paragraph << endl;

// Converts string
for (int i = 0; i < paragraph.length(); i++) {
if (!isspace(paragraph[i])) {
newParagraph.push_back(paragraph[i]);
} else {
if (!isspace(paragraph[i - 1])) {
newParagraph.push_back(' ');
}
}
}

// Outputs string todata file
ofstream newOfs("data.exe", ios_base::binary | ios_base::out);

if (newOfs.is_open()) {
newOfs << newParagraph;
newOfs.close();
}

cout << newParagraph;

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS