Help to create an algorithm for solving the problem, the source line needs to be changed. I want to try to write the code myself,
Closed due to the fact that the essence of the issue is incomprehensible by the participants Vladimir Martianov , aleksandr barakin , D-side , Pavel Parshin , Grundy 13 Apr '16 at 17:38 .
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 .
- oneIf it's not about training, directly, but about implementation, then look towards std :: regex_replace - Evgeny Borisov
4 answers
Well, a rough idea: we go along the line from left to right, we support 3 variables - a pointer to the current character (cur), a pointer to the written character (wrt) and the number of open brackets (cnt).
The initial values are cur = wrt = pointer to the beginning of the line. cnt = 0.
Если текущий символ - открывающая скобка, то cnt++ Если количество открытых скобок равно 0, то мы пишем *cur в *wrt, wrt++ cur++ Если текущий символ - закрывающая скобка - то cnt-- Если *cur = 0 то выход. Выполнить сначала. - It is only necessary to take into account that the brackets may not coincide - such as
aa(bb(cc(dd)ee. - Harry - as I understand it, then you need to delete in your case (bb (cc (dd) - Ilya
- I left it to think of the author of the question, much depends on the exact wording or refinement of the teacher. Corrected literally 1 word. - pavel
There are standard search functions for substrings in a string. You look for the first opening bracket and memorize its index, then in the same way you close the index and also memorize the index. The fact that between the indices can also be obtained by the standard copying functions of the adjustment starting and ending with a certain position. This will be the text between the brackets. Do with it what you want and repeat the algorithm. Also for convenience, you can use another line in which you will add the result of your actions. I think . option with another array is the best option. Go through the main array and check the conditions, then what satisfies add to another array
- strchr - Returns a pointer to the first occurrence of the low byte of the specified parameter. strrchr - Returns a pointer to the last occurrence of the low byte of the specified parameter in the string. Did you mean these functions? - Ilya
- I am not a sishnik, I proposed only an algorithm, and such functions exist in all languages, but are called differently. - gregor
In short, I could not change this line and I went the other way. I copied the content to another line, only there is, let's say, one big question. If the line does not contain a closing parenthesis, then I have garbage, or rather garbage after the character '('.
#include <iostream> #include <cstdio> using namespace std; int main() { setlocale(LC_ALL, "RUSSIAN"); char str[100] = "# :#($ afe)0t:(rk: f)akt", *ptr; char n_str[100] = "\0", *n_ptr; cout << "Данная строка: " << str << endl; ptr = str; while (*ptr) { n_ptr = n_str; while (*ptr != '(' && *ptr) { *n_ptr = *ptr; n_ptr++; ptr++; //игнорируем символы которые стоят между скобок //и сами скобки тоже игнорируем if (*ptr == '(') { *ptr++; while (*ptr != ')') ptr++; ptr++; //перемещаемся за за символ ')' } } } *n_ptr = '\0'; cout << "Новая строка: " << n_str << endl; return 0; } - Apparently you did not quite understand the idea of how to do this in line 1. Here is ideone.com/mfD087 my implementation. - pavel
- Thank you for the work done. - Ilya
I think if it is intended to delete all completed sequences in brackets, then an algorithm using the stack for “recognizing” characters in brackets and a “queue” in which we place pairs of indices corresponding to the opening and matching closing brackets will be appropriate.
I write “queue” in quotation marks, because if the index of the opening bracket in a pair's queue is less than the index at its end, then the “queue” turns into a “stack” (from the side where the elements are usually inserted into it), t . A new pair of indexes (in fact, this is just the closed sequence of characters in brackets, which began earlier than the one already placed in the queue) replaces the elements previously placed in it.
We get this algorithm.
Looking through the characters of the string, when the opening bracket appears, we put its index on the stack. The closing bracket extracts from the stack the index of the paired opening and together they form a pair, which must be placed in the queue, taking into account the remark above.
Those. at the end we get an array ("queue") of pairs of character indexes, which should be. removed from the string. In fact, the deletion can be replaced by the "in-place" line rewriting, excluding characters from the pairs in this array.
(as always, the description is longer than the code and perhaps more incomprehensible -))
PS
@Ilya, you wrote that you want to write the code yourself, if it will still be interesting, scribble in the comments (indicating @avp (otherwise I will not see the notification (such is crap here))).
- Thank you for the idea, but I haven’t reached the queue and the stack yet, I read Schildt for a couple of months - Ilya
- Well, it's simple. The stack (can be implemented in different ways), for example, this is an array at the end of which we add new elements and select them from there. Those. add to a [0], a [1], a [2] and then get a [2], a [1] and at this moment there remains a [0], if we add it again, then we put in a [1]. And we always add to the queue at the end and always choose from the beginning. Those. claudem a [0], a [1], a [2], we get a [0], a [1] ... The stack is naturally implemented in C ++ with the container std :: vector, and since the "queue" in your task is one-time then the same vector is fine. - avp
- @avp is not sure that there will be a queue here, but the stack will come in handy. - pavel
- @pavel, I wrote a "queue". In the sense that the indices of previously appeared external (provided that they are closed) brackets will be at its beginning. Actually, in the deletion phase, this is a logical queue of character ranges to be deleted. (no, I don’t know how to lucidly describe algorithms ...) - avp