CreateaclassRunLengthEncodethatcontainsthemethod encode which takes one argument: a String to be encoded as described below.
Solve the following in C##
-
Manytimes,certaindatafiletypescanconsistoflarge amounts of repeated data. For instance, images can have large runs of the same color. This can be easily compressed using a technique called run length encoding. With run length encoding, large amounts of repeated data are stored as the repeated data and the number of times to repeat it.
-
CreateaclassRunLengthEncodethatcontainsthemethod encode which takes one argument: a String to be encoded as described below.
-
ThereturnvalueshouldbeaStringwhichhasbeenencoded with the following
algorithm : -
Ifanycharacterisrepeatedmorethan4times,theentire set of repeated characters should be replaced with a slash '/', followed by a 2-digit number which is the length of the set of characters, and the character. For example, "aaaaa" would be encoded as "/05a". Runs of 4 or less characters should not be replaced since performing the encoding would not decrease the length of the string.
Notes
-
Letters are case sensitive. For example "AaAaAa" cannot be encoded.
-
You may only encode repeats of a single character, repeats of multiple characters cannot be encoded. For example "ababababab" cannot be encoded as "/05ab".
Constraints
-
input will have between 0 and 50 characters, inclusive.
-
input will consist only of letters 'a' - 'z' and 'A' - 'Z',
digits '0' - '9', the space character, and the characters in the following string: "{}[]():;'+=.,". (quotes are for clarity only and cannot be in the input string)
Examples
a) "aaaaa"
a.Returns: "/05a"
b. The example stated above.b) "aaaa"
a. Returns: "aaaa"
b. Remember not to encode runs of length 4 or less.c) "abcabcabcabcabc"
-
Returns: "abcabcabcabcabc"
-
Do not encode repeated segments of more than one
character
d) "if(a){if(b){if(c){if(d){if(e){5 deeeeeeep}}}}}"
a. Returns: "if(a){if(b){if(c){if(d){if(e){5 d/07ep/05}" e) ""
-
ALGORITHM:
1. Initialize three variables namely 'output', 'count', and 'current' to store the string, to keep track of the number of consecutive characters, and to keep track of the current character.
2. Return the input string if its length is less than or equal to 4.
3. Use for loop and iterate over the input string from the second character and increment the count by 1 if the current character is the same as the character in the 'current' variable.
4. If the current character is not the same as the character in the 'current' variable then check if the count is greater than 4. If it is greater than 4, add "/" + count as a string with leading zeros ('count.ToStrin("D2")') + the current character to the output string. If it is not greater than 4 then add the current character repeated 'count' times to the 'output' string.
5. When the loop completes check if the count of the last character is greater than 4 or not. If yes, add "/" + count as a string with leading zeros ('count.ToString("D2")') + the current character to the output string. If not, add the current character repeated 'count' times to the 'output' sting.
6. After that return the 'output' string.
7. Read the input string, encode it using the 'Encode" method and print the encoded string in the main metho.
Step by step
Solved in 4 steps with 4 images