There is a character C and the lines S1, S2. Before each entry of the character C in line S1, you must insert the line S2. How to do it? Let's say it will be inserted "(\ /) O_O (\ /)"

Closed due to the fact that the essence of the question is incomprehensible by the participants Kirill Stoianov , Harry , iluxa1810 , Visman , pavel Nov 7 , Nov. at 6:08 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • On this forum, tasks are not performed instead of the author - nick_n_a
  • The author would not have asked if he was just too lazy. It’s just that I either have a pseudo-graphic climbs or simply isn’t inserted - Sergei Medvinsky

2 answers 2

There are several approaches to solving this problem. But all of them can be divided into two main cases: an approach in the spirit of the C language, when you need to create a new character array for the resulting string, and a second approach in the spirit of the C ++ language, when you are dealing with objects of the std::string class.

C language approach

 #include <iostream> #include <cstring> int main() { const char *s1 = ">< ><"; const char *s2 = "(\\/)O_O(\\/)"; char c = '<'; size_t n1 = std::strlen( s1 ); size_t n2 = std::strlen( s2 ); size_t k = 0; for ( const char *p = s1; ( p = std::strchr( p, c ) ) != nullptr; ++p ) { ++k; } char *result = new char[ n1 + k * n2 + 1 ]; char *p = result; const char *q = s1; do { if ( *q == c ) { memcpy( p, s2, n2 ); p += n2; } *p++ = *q; } while ( *q++ ); std::cout << s1 << std::endl; std::cout << s2 << std::endl; std::cout << result << std::endl; delete [] result; return 0; } 

The output of the program to the console:

 >< >< (\/)O_O(\/) >(\/)O_O(\/)< >(\/)O_O(\/)< 

C ++ approach

 #include <iostream> #include <algorithm> #include <string> #include <cstring> int main() { std::string s1( ">< ><" ); const char *s2 = "(\\/)O_O(\\/)"; char c = '<'; std::cout << s1 << std::endl; size_t n2 = std::strlen( s2 ); auto k = std::count( s1.begin(), s1.end(), c ); s1.reserve( s1.size() + k * n2 ); for ( std::string::size_type pos = 0; ( pos = s1.find( c, pos ) ) != std::string::npos; pos += n2 + 1 ) { s1.insert( pos, s2 ); } std::cout << s2 << std::endl; std::cout << s1 << std::endl; return 0; } 

The output of the program to the console:

 >< >< (\/)O_O(\/) >(\/)O_O(\/)< >(\/)O_O(\/)< 

In this case, you can change the source string itself s1 .

    Like this :)

     using namespace std; string s1 = "Вот твое задание "; string s2 = ", мля,"; char d = ' '; int main(int argc, const char * argv[]) { for(int i = 0; i < s1.length(); ++i) { if (s1[i] == d) { s1.insert(i,s2); i += s2.length(); } } cout << s1 << endl; } 
    • Rehash, but there is an aesthetic moment. Half a week, I’m dragging a comma which seems not to be at the end of a sentence. - Alex.B
    • @ Alex.B :) Well, fix the last space in the source line to the exclamation mark :) - Harry