So i have written a code to encode a string of a select amount of letters into huffman code.
that works fine, but now i want to decode it, but I don't know how to.
here's what I want to do in psuedo code.
look at huffman code
take first number
while(there is still more huffman code to examine){
(if value were looking at in the huffman code == any of the huffman letter codes){
add the letter to a string variable
move on to the next number in the huffman code
}
else{
the current huffman code value you are looking at should have the next number in the code added to it
}
}
return theNowDecode code.
But I have no clue how to go about this. Please give me a push.
thanks
Code:
#include <iostream> #include <string> using namespace std; string Huffman(char letter); string Huffman_word(string word); string Huffman_code(int code); string word; int main() { string word; cout<<"Please Enter Your String: "; getline(cin,word); cout<<Huffman_word(word); } string Huffman_word(string word) {//This function takes a word and returns its Huffman encoding string Huffman_code; int n = word.length(); for(int i = 0; i<n; i++) { Huffman_code.append(Huffman(word[i])); } return Huffman_code; } ////////////DECODE/////////////// ////////////ENCODE/////////////// string Huffman(char letter) {//this function takes a letter and produces it's corresponding Huffman encoding switch(letter){ case 'a': case 'A': return "00"; break; case 'e': case 'E': return "000"; break; case 'f': case 'F': return "1101"; break; case 'h': case 'H': return "1011"; break; case 'i': case 'I': return "1000"; break; case 'm': case 'M': return "0111"; break; case 'n': case 'N': return "0010"; break; case 's': case 'S': return "1011"; break; case 't': case 'T': return "0110"; break; case 'l': case 'L': return "11001"; break; case 'o': case 'O': return "00110"; break; case 'p': case 'P': return "10011"; break; case 'r': case 'R': return "11000"; break; case 'u': case 'U': return "00111"; break; case 'x': case 'X': return "10010"; break; case ' ': return "111"; break; } }
that works fine, but now i want to decode it, but I don't know how to.
here's what I want to do in psuedo code.
look at huffman code
take first number
while(there is still more huffman code to examine){
(if value were looking at in the huffman code == any of the huffman letter codes){
add the letter to a string variable
move on to the next number in the huffman code
}
else{
the current huffman code value you are looking at should have the next number in the code added to it
}
}
return theNowDecode code.
But I have no clue how to go about this. Please give me a push.
thanks
Comment