Created
March 27, 2011 04:02
-
-
Save prudhvi/888899 to your computer and use it in GitHub Desktop.
Encodes the input string into a compressed format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Please enter the string" | |
input = gets.chop | |
currentChar = input[0] | |
compressedString = "" | |
currentCharCount = 1 | |
wordSize = input.size | |
count = 1 | |
input[1..wordSize].each_char do |c| | |
count = count + 1 | |
if(currentChar == c) | |
currentCharCount = currentCharCount + 1 | |
end | |
if(currentChar != c or count == wordSize ) | |
compressedString = compressedString + currentChar | |
if(currentCharCount != 1) | |
compressedString = compressedString + currentCharCount.to_s | |
end | |
if(count == wordSize and currentChar != c) | |
compressedString = compressedString + c | |
end | |
currentChar = c | |
currentCharCount = 1 | |
end | |
end | |
puts compressedString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment