Answer the question
In order to leave comments, you need to log in
How to disable the replacement of punctuation marks when outputting text from a C++ file?
It should output the text from the file, swapping every two adjacent words.
Here's what she does.
How to make it so that she leaves periods and commas (other punctuation marks too, a question mark, etc.), but changes words and how to make her read words from a new line and swap them (in the screenshot it can be seen that in the text file I put a space from a new line (if it weren’t there, the word would not change))
The task is to leave dots and other signs in their place
Answer the question
In order to leave comments, you need to log in
You can read character by character and compare each character with delimiters. Add tokens (found words and delimiters) into an array and output as it is filled in the desired order.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, const char * argv[]) {
ifstream file("file.txt");
if (!file) {
std::cout << "Unable to open file" << std::endl;
}
string separators = " \t\r\n,.!?;:";
int i = 0;
char c;
bool lastWasSep = true;
std::string tokens[4];
std::string curToken;
while (file.get(c)) {
bool isSep = separators.find(c) != string::npos;
if (lastWasSep == isSep) {
curToken += c;
} else {
tokens[i++] = curToken;
curToken = c;
}
lastWasSep = isSep;
if (i == 4) {
cout << tokens[0] << tokens[3] << tokens[2] << tokens[1];
i = 0;
}
}
tokens[i++] = curToken;
if (i == 4) {
cout << tokens[0] << tokens[3] << tokens[2] << tokens[1];
} else {
for (int j = 0; j < i; j++) {
cout << tokens[j];
}
}
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question