You are here:

C/String compare

Advertisement


Question
QUESTION: Hi I hope you can help. I need to figure out how to sort birth months in c programming. Right now I'm trying to figure out how to assign the month value as the month input is read in from the file. First I do string compare, like for instance say the input month is May, then I compare that to a char variable[i] thats a string of all of the months. Once it finds May in there I'm trying to assign another variable I have 5 so the computer knows that May is the fifth month. That way I can sort it easier.
For some reason the for loop runs beyond MAX point.
here's my code below:

for(i=0; i<people; i++){
         fscanf(fp, "%s", students[i].first);
         fscanf(fp, "%s", students[i].last);
         fscanf(fp, "%s", students[i].month);
         //for(k=0; k<=12; k++){
         //   if(!strcasecmp(students[i].month, months[k]))
         // {  // break;
         //  monthVal[i] = k + 1;
         //k = 12;
         //break;}
         //}
         
         // monthVal[i] = k + 1;
         printf("Month value: %d and i = %d\n", monthVal[i], i);
         fscanf(fp, "%d", &students[i].day);
         fscanf(fp, "%d", &students[i].year);
         }
         //break;

Why does i go all the way to 11, when its suppose to stop once its no longer less than people?

ANSWER: Hello Tiffany

From the code you have shown me, I would say that withing the loop
for(i=0; i<people; i++)
i will go from 0 up to but not including people, and will be equal to the value of people once the loop quits. If you see i going up to 11 inside the loop, then I think you must have people set to 12. Please check your program or post the entire thing. You can print people just before the loop to check its value.

For checking the month strings, I suggest you put the logic into a separate function which has the job of accepting a month name and returning a month value. Separating your program into functions will make it much less confusing. When checking a month against your array of months, be sure that k starts at 0, and goes to 11, not 12. If the months array starts at 0, the "January" is at months[0], and "December" is at months[11]

The function is something like this:
int MonthToNumber(char* month)
{
  for(k=0; k<12; k++){ // not k<=12
     // perhaps compare just the first 3 characters with
     // strncasecmp(month, months[k], 3)
     if(!strcasecmp(month, months[k])) return k+1;
  }
  // if we get to this point none of the months in the array
  // matched the month passed in, so there is some error.
  return 0;
}

Then you call it like this:

fscanf(fp, "%s", students[i].month);
monthVal[i] = MonthToNumber(students[i].month);

I have not run my code through a compiler, so there may be an occasional syntax error but I'm just trying to give you a general idea of what to do.

Don't forget to check if the returned value is 0.

I hope that helps you out.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Thank you for the help. Your code was very helpful. I used that concept in my program but for some reason its just returning 0. I did some print statements to check to see that the month was being read into the function correctly it is, but the function is just returning 0. Could you take a look at my code? its below.

//in main
monthVal[i] = MonthToNum(students[i].month);
printf("Month value: %d and i = %d\n", monthVal[i], i);

//the function itself
int MonthToNum(char * month){
   int k;
   char * months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
         "November", "December"};
   printf("This is the month: %s\n", month);
   for(k = 0; k<12; k++){
         if(!strncasecmp(month, months[k], 3))
         { return k+1;}
   else{return 0;}
    }
}

Answer
Hi Tiffany.

The "else return 0" is happening as soon as the first string compare fails. You want it to happen if all string compares fail.



Your code is this

for(k = 0; k<12; k++){
if(!strncasecmp(month, months[k], 3)) { return k+1;} else{return 0;}

Notice that the else is part of the if, which is done on each iteration of the loop.

The code should be

for(k = 0; k<12; k++)
{
  if(!strncasecmp(month, months[k], 3))
  {
     return k+1; // return on the first successful string compare
  }
} // end the loop here
// if we got to this point, none of the string compares passed
// so we return 0
return 0;

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.