Skip to content

Instantly share code, notes, and snippets.

@prudhvi
Created March 27, 2011 04:02
Show Gist options
  • Save prudhvi/888899 to your computer and use it in GitHub Desktop.
Save prudhvi/888899 to your computer and use it in GitHub Desktop.
Encodes the input string into a compressed format
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