I'm tyring to write a simple LZW compression program the goal is to compress 20 repeats of ABBA. I've run into trouble when it comes to implementing the algorithm in C++.
My problem is comparing a string called CODE (which contains some of the characters from the input ABBA) with another character or string K which looks at the next letter from the input string. I need to merge these CODE and K and compare these with the entries in the dictionary to see if they exist. The example below should make it clearer
ABBA
Initialize dictionary 0:A 1:B 2:C 3
1.K=A in dictionary? Yes
2.CODE <-K
K=B
is AB ([CODE]K) in dictionary? No
add [CODE]K to dictionary 4:AB
output the numerical representation of code to output stream (1 (for B))
3.Code now becomes B (CODE<-K)
K=next char (B)
is BB in dictionary N etc...
If anyone has an idea of how i can implement code and K such that i can compare them as above then I would be eternally grateful.
Thanks