
complete the string processing if (k = 1)

S2 = s2 + k.ToString() + c.ToString() // s2 = s2 + "AAA' = s2 + "3A" // go to the next recursion level, continue processing the string return ConvertStr(s, pos + 1, s, 1, s2) otherwise, put the number k in front of the character c if (k = 1) if k = 1, then simply add the symbol c to s2, return ConvertStr(s, pos + 1, s, k + 1, s2) Įlse // if the characters are different, then fix the temporary result in the s2 line If (s = c) // if the next and previous characters are the same, then move on. If (pos = 0) // first character of string // go on to the next symbol return ConvertStr(s, pos + 1, s, 1, s2) Įlse // not the first character of a string recursive string processing function static string ConvertStr( string s, int pos, char c, int k, string s2) This is necessary because the characters that occur once in a row should be handled in a special way. a variable c of type char, which represents the character at the previous recursion level.an integer k specifying the number of repetitions of the symbol s at a given recursion level.a variable that determines the position of the character in string s at a given recursion level.string s of type string that is being processed.The recursive function contains the following parameters: Depending on the position of the current symbol being processed, the corresponding recursive function call occurs. Each recursive call processes one character of the string.

The recursive function ConvertStr() recursively scans the entire string. In this sample, you can develop recursive functions that process strings by any rules. This problem is very conveniently solved using recursion. Example of converting the string “AAABCCCCAADDDEF” => “3AB4C2A3DEF”
#NAMECHANGER RECURSIVE SERIES#
Example of calculating the sum of a seriesĭevelop a recursive function to calculate the sum of a series Therefore, it is important that the recursive method be designed in such a way that it declares the minimum possible number of parameters and local variables. In this case, the CLR will generate the corresponding exception.
#NAMECHANGER RECURSIVE CODE#
The compiler builds the code of the recursive function in such a way that the temporary values of local variables are automatically saved with each recursive call when calling a recursive function, it is not necessary to additionally save the temporary values of local variables.For many tasks, recursion provides the following interrelated advantages: Recursion is always compared with iteration. What are the advantages of using recursion in programs? when returning from the recursive method (the return operator), old local variables and parameters are restored, as well as their values at the call point of this method.These local variables and parameters receive new initial values. the program code of the method is executed first with new local variables and parameters.system stack allocates memory for new local variables and parameters.If the method calls itself, then the following processes occur in memory: The sequential process of recursive method calls is similar to a cyclic process. In a recursive method, the same method is called using it’s name. Otherwise, a memory overflow will occur and the program will “hang” without reaching the calculation of the required result.Ī recursive method is a method that calls itself.

Recursive method calls must end when a certain condition is reached. Recursion is the development of a method in such a way that it calls itself. What is recursion? What is a recursive method (function)?
