Page 383 - Programming Microcontrollers in C
P. 383
368 Chapter 7 Advanced Topics
*s=0;
return i;
}
Listing 7-7: Huffman Decoding Data
The above function is tested in conjunction with the encode routine
with the following relatively simple program. In this code, provision
is made to enter a line of text from the computer keyboard. This text
is terminated when a new line character is detected. These data are
then sent to the encode routine. The encode routine returns the encoded
data in the array array[].This array is passed to the decode routine.
The return information from decode is contained in the array s[].
This string is then printed out to the screen.
#include <stdio.h>
#define ARRAY_SIZE 100
int decode(unsigned M[],char *s);
int encode(char *a,unsigned *array,int length);
main()
{
char a[ARRAY_SIZE] ;
int c,i=0;
unsigned array[ARRAY_SIZE];
char s[ARRAY_SIZE];
while((c=getchar())!=’\n’)
a[i++]=c;
a[i]=’\n’;
encode(a,array,ARRAY_SIZE);
decode(array,s);
printf(“%s”,s);
}
Listing 7-8: Encode / Decode Test Routine
The above program echoes the input string to the computer screen.
All lower-case letters are converted to upper case in the process.