CreateaclassRunLengthEncodethatcontainsthemethod encode which takes one argument: a String to be encoded as described below.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Solve the following in C##

Run Length Encode
  • 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"

    1. Returns: "abcabcabcabcabc"

    2. 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) ""

Expert Solution
Step 1

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.

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Types of Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education