Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU SMBU

Electronic problem book on programming

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2026

 

PT for STL | Task groups | STL4Str

PrevNext


Strings as Sequence Containers

In all tasks of this group, it is required to process a string, considering it as a sequential container and using the constructors of the string class, its member functions insert and erase, and/or suitable generic algorithms. Auxiliary strings should not be used when solving the tasks.

STL4Str1°. Given a positive integer N and a character C. Using the constructor of the string class, fill the string S by writing N copies of the character C into it.

STL4Str2°. Given a string S0 of even length. Using the string class constructor with iterator parameters, write to the string S the second half of the characters of the string S0 in reverse order.

STL4Str3°. Given a string S of even length N (N ≥ 4). Using a suitable generic algorithm twice, swap the first and last characters, as well as the two middle characters of the string S.

STL4Str4°. Given an integer K (> 0) and a string S of even length. Using one call to the member function insert with an iterator parameter, add K characters "*" to the middle of the string S.

STL4Str5°. Given an integer K (> 0) and a string S, whose length is greater than K. Using one call to the member function insert with iterator parameters, add to the end of the string S its first K characters in reverse order.

STL4Str6°. Given an integer K (> 0) and strings S1 and S2. The string S1 contains at least K characters, and the string S2 — at least two characters. Using two calls to the member function insert with iterator parameters, transform the string S2 by inserting after its first character the first K characters of the string S1 in the original order, and before its last character — the last K characters of the string S1 in reverse order.

STL4Str7°. Given a string S. Using the member function insert in a loop with an iterator parameter, insert the character "*" before each character of the string S.

STL4Str8°. Given a string S, containing at least two characters. Using the member function insert in a loop with an iterator parameter, insert after each character of the string S with an even ordinal number (numbering from 1) three characters "*". For example, the string "abcde" should be transformed into "ab***cd***e".

STL4Str9°. Given a string S of even length. Using the member function insert in a loop with an iterator parameter, duplicate each character from the first half of the string S (for example, the string "abcdef" should be transformed into "aabbccdef").

STL4Str10°. Given a string S of odd length N (N ≥ 5). Using one call to the member function erase with iterator parameters, remove the three middle characters of the string.

STL4Str11°. Given a string S, containing at least three characters. Using the member function erase in a loop with an iterator parameter, remove every third character of the string S.

STL4Str12°. Given a string S, containing at least three characters. Using a suitable generic algorithm in a loop, move every third character of the string S to its end, arranging the moved characters in the original order.

STL4Str13°. Given a string S, whose length is divisible by 3. Using a suitable generic algorithm, move the second third of the characters of the string S to its end (preserving the order of characters).

STL4Str14°. Given a string S, containing at least three characters "*". Using three calls to a suitable generic algorithm and two calls to the member function erase, remove in the string S the second and the last of these characters.

STL4Str15°. Given a string S. Using two calls to a suitable generic algorithm and one call to the member function erase, remove all characters located in the string S between the first and last digit. If there are fewer than two digits, do not change the string.

STL4Str16°. Given a string S of even length. Using a suitable generic algorithm in a loop and the member function erase, remove in the second half of the string S all characters that match the characters from its first half.

STL4Str17°. Given a string S. Using a suitable generic algorithm and the member function erase, remove all adjacent digit and letter characters, leaving in each set of consecutive digits or letters only the initial character. For example, the string "123abc456de" should be transformed into "1a4d". Assume that the string S contains only Latin letter characters.

STL4Str18°. Given an integer K (> 0) and a string S, whose length exceeds the number K. Using a suitable generic algorithm and the member function insert, duplicate in the string S the first of the substrings containing K consecutively located digits. If the substring contains more than K digits, then duplicate the initial K digits of this substring. If there are no required substrings, do not change the string.

STL4Str19°. Given strings S1 and S2; the length of string S1 does not exceed the length of string S2. Using a suitable generic algorithm and the member function erase, remove in the string S2 the last substring that matches the inverted string S1 (do not change the string S1). If there are no required substrings, do not change the string S2.

STL4Str20°. Given a number N (1 ≤ N ≤ 26) and a string S. Using two calls to a suitable generic algorithm with an insert iterator, add to the beginning and to the end of the string S N first uppercase letters of the Latin alphabet, arranging these letters at the end of the string in alphabetical order, and at the beginning — in reverse alphabetical order. For example, for N = 3 and S = "567", the required string is "CBA567ABC".

STL4Str21°. Given an integer K (> 0) and a string S of even length, greater than 2K. Using two calls to a suitable generic algorithm, perform in the first half of the string S a cyclic shift of characters by K positions to the left, and in its second half — a cyclic shift of characters by K positions to the right.

STL4Str22°. Given a string S of even length. Using two calls to a suitable generic algorithm, replace in the first half of the string S all digit characters with the character "*", and in its second half — all uppercase Latin letters with the character "_".

STL4Str23°. Given a string S. Using one call to a suitable generic algorithm, change the case of all letter characters of the string S to the opposite, and replace digit characters with the character "*".

STL4Str24°. Given strings S1 and S2 of the same length. Using a suitable generic algorithm, transform the string S2 by replacing with the character "*" those of its characters that do not match the characters of the string S1 located in the same positions. For example, in the case S1 = "ab2CD" and S2 = "as2Cd", the string S2 should be transformed into "a*2C*".

STL4Str25°. Given a string S. Using a suitable generic algorithm, copy to the end of the string S all digit characters contained in it in the same order.

STL4Str26°. Given a string S. Using a suitable generic algorithm and the member function erase, remove in the string S all digit characters.

STL4Str27°. Given a string S. Using a suitable generic algorithm, regroup the elements of the string S by moving all digit characters to its beginning in the same order.

STL4Str28°. Given a string S of even length. Using two calls to a suitable generic algorithm, sort the first half of the string S in descending order of character codes, and the second half — in ascending order.


PrevNext

 

  Ðåéòèíã@Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
01.01.2026