r/learnprogramming•
[deleted]

strncmp isn't working for me

Im not sure what I've got wrong here, the rest of it seems to be working fine:

              /*this code takes an input string from a file or standard input and creates a smaller
              file by turning the text into code. It reads 5 characters at a time then, if there is
              an existing codebook entry for that stiring, it does nothing with codebook but only
              prints the existing symbol associated with the string. If there is no codebook entry,
              it saves it to the codebook string and assigns it a symbol and prints that symbol.
              upon completion, it prints a series of three counts. First the total number of characters
              run through, then the total number of codebook entries (max 90) and last the number of
              characters printed. */




                            #include <string.h> //declares strcpy and strlength and strcmp

                            #include <stdio.h>
                            int CodeBook_check(char[], int, char[], 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][5]; //declare array codebook

                            while (fgets(st, 5, myfile)!=0) //this while loop gets the characters 5 at a time/
                            {

                            u=j;// setting up a variable for comparison
                            CodeBook_check(st, z, CodeBook[j], j); //this sub routine checks the string to see if there is a previous entry
                            if (j==u) //if the code has incremented
                            {
                             //printf("%c", z);
                             //printf("%d", j);// this is just checking that j is incrementing
                             z++;
                             codebook +=1;
                                    }
                                        count +=1;
                                    if (count>=90)
                                        {
                                   while (fgets(st, 5, 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[], 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;
                            }

My input file is text.txt that reads the following: CHAPTER 1 1 In the beginning God created the heaven and the earth. 2 And the earth was without form, and void; and darkness [was] upon the face of the deep. And the Spirit of God moved upon the face of the waters. 3 And God said, Let there be light: and there was light. 4 And God saw the light, that [it was] good: and God divided the light from the darkness. 5 And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. 6 # And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. 7 And God made the firmament, and divided the waters which [were] under the firmament from the waters which [were] above the firmament: and it was so. 8 And God called the firmament Heaven. And the evening and the morning were the second day.

My problem is that it's simply not going into the strncmp and I'm not sure what I'm doing wrong.

Expected Output:

§!"#$%&'()*+,-./012./3456789:;<=>?@ABCADE.FGCHIJKLMNOPQRSTUVWXYZ;W[]1^ST_`abcdefghij$YklA<=mnSop$Yqr;A<=stuvE.wxy.e§morning§were§tLirst§daym6§#§And§Hsaid,§Let§there§be§a§firmament§in§the§midst§NOPs,§and§let§it§divide$§waters§from$§waters.17§AnSd§made§tLirmament8d§divide.[ters§which§[were]§under§Afirmament§from§tOPs§which§[were]§above$§firmament:§;it§was§so.¶ 8§AnSop$§firmament§H,n.§And§the§evening§and§the§morning§were§Asecond§day.¶ ¶ 839§90§435¶

Actual Output:

§!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxydarkness.¶ 5§And§God§called§the§light§Day,§and§the§darkness§he§called§Night.§And§the§evening§and§the§morning§were§the§first§day.¶ 6§#§And§God§said,§Let§there§be§a§firmament§in§the§midst§of§the§waters,§and§let§it§divide§the§waters§from§the§waters.¶ 7§And§God§made§the§firmament,§and§divided§the§waters§which§[were]§under§the§firmament§from§the§waters§which§[were]§above§the§firmament:§and§it§was§so.¶ 8§And§God§called§the§firmament§Heaven.§And§the§evening§and§the§morning§were§the§second§day.¶ ¶ 839§90§578¶

§=space except for the first one ¶=new line

Thanks for any advice!

Posted Mar 24, 2012, 11:44 PM UTC

13
Loading comments…