Hello. Will anyone help solve and parse the code in detail and explain? By study set. Assignment text: Given a text consisting of several sentences. Sentences end with a period, exclamation, or question marks. From each sentence remove the middle character if the length of the sentence is odd, and two middle characters if the length is even.

As I understood, here it is necessary to count the Length to one of the characters (.!?) And already in each segment count the number of characters and remove the averages. So I do not understand how to implement. Thank you in advance!

  • 2
    Yes something like that. Cycle through the text. First, the pointer to the beginning of the line is the pointer to the beginning of the first sentence. Looking for a sign (!?.) - a pointer to the end. The difference is the length, delete the character (s). Next, the pointer to the beginning of the second sentence is the first non-blank character from the end. Look for the sign again (!?.) And so on until the end of the text. - Alexey Sarovsky

1 answer 1

#include <iostream> #include <cstring> #include <signal.h> #include <memory> #include <string.h> #include <string> #include <vector> int main(int argc, char** argv) { std::string source = "Hello world ! My name is stackowerflow."; std::vector<std::string> tokens = {"!","?","."}; for(std::string token : tokens){ size_t pos = source.find(token); if( pos != std::string::npos){ source.erase(pos/2,1); } } std::cout<<source<<std::endl; return 0; } 
  • Yes, Dijkstra ( EWD ) was not mistaken ... - avp
  • @avp, what do you mean by this? - Yuri Solovyov
  • The fact that such an algorithm for the selection of sentences (by the way, there may be several sentences that end with the same symbol) is extremely ineffective - avp