I am having some trouble getting my program to make arrays of the right length. Our assignment is to make functions called "category1", "category2" and so on, all of which analyzes a char array and determines if the passed char array is in the specific category.
The "category" that I am having trouble with determines if the first third of the char array appears elsewhere in the whole array. Here's my code so far.
I pass a 6 character string each time, and the length that I get for the array I created in the function (comp) comes out to 5. Does anyone know why this is happening?Code:int Category4 (char s1[]) { int ndx, ndx2; char comp[strlen(s1)/3]; for (ndx = 0; ndx < strlen(comp); ndx++) { comp[ndx] = s1[ndx]; } for (ndx = strlen(comp); ndx < strlen(s1); ndx++) { if (s1[ndx] == comp[0]) { for (ndx2 = 0; ndx2 < strlen(comp); ndx2++) { if (comp[ndx2] != s1[ndx+ndx2]) { break; } if (ndx2 == strlen(comp)) { printf("%s is in category 4.\n", s1); return 1; } } } } return 0; }



LinkBack URL
About LinkBacks


