Created
March 24, 2012 23:54
-
-
Save Katharine/2189397 to your computer and use it in GitHub Desktop.
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
#include <string.h> //declares strcpy and strlength and strcmp | |
#include <stdio.h> | |
int CodeBook_check(char[], int, char[][6], int); /*function declaration*/ | |
int main (int argc, char *argv[]) | |
{ | |
FILE * myfile;//make a file name variable | |
if (argv==0) //choosing between stdin and file | |
{ | |
myfile = stdin;//if stdin has input | |
} | |
else | |
{ | |
myfile=fopen("text.txt", "r"); //if no stdin, open file | |
} | |
char st[361]; //declare the character string | |
int i = 0, j=1; //declare variables | |
char z='!'; | |
z--; | |
int count = 0; // create a count, count will tell me how many codebook | |
int end_count = 0, u=0;//declare variable end_count | |
int codebook =0;//declare variable codebook | |
char CodeBook[90][6]; //declare array codebook | |
while (fgets(st, 6, myfile)!=0) //this while loop gets the characters 5 at a time/ | |
{ | |
u=j;// setting up a variable for comparison | |
j = CodeBook_check(st, z, CodeBook, j); //this sub routine checks the string to see if there is a previous entry | |
if (j!=u) //if the code has incremented | |
{ | |
//printf("%d", j);// this is just checking that j is incrementing | |
z++; | |
codebook +=1; | |
} | |
count +=1; | |
if (count>=90) | |
{ | |
while (fgets(st, 6, myfile)!=0)//this while loop gets the characters 5 at a time/ | |
{ | |
printf("%s", st); | |
end_count+=strlen(st); | |
codebook+=strlen(st); | |
} | |
} | |
end_count+=strlen(st); | |
} | |
if (!(st[i] == '\n')) //this is a statement added to check for an empty file. | |
{ | |
printf("\n"); //if there is one, it prints a null | |
} | |
fclose(myfile); | |
printf("%d %d %d", end_count, count, codebook); | |
return 0; | |
} | |
/*This codebook subroutine checks the letters of the string against all existing | |
strings in the codebook to see if there is a match. if so, it inserts the exhisting code. if not, it | |
creates a new code*/ | |
int CodeBook_check(char st[], int z, char CodeBook[][6], int j) | |
{ | |
int i=0;//declare a counter for the for loop | |
int f=strlen(st); // find string length for the strncpy and for loop | |
int p; // declare variable p for the for loop counter | |
for (i=0; i<j; i++) | |
{ | |
if (strncmp(CodeBook[i], st, f)==0) //if string compare is the same | |
{ | |
z-=i;//adjust the z value to output the appropiate symbol | |
printf("%c", z); | |
return j; //return the new z value | |
} | |
} | |
printf("%c", z); | |
strncpy(CodeBook[j], st, f); //add word to code book | |
j++; //increment codebook pointer | |
return j; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment