Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the strcmp() function in One month reminder progam

Tags:

arrays

c

Here is the program that prints a one month reminder list. This is an example from K.N. King book. My problem is that I can't understand about how strcmp function works in this program.

#include <stdio.h>
#include <string.h>

#define MAX_REMIND 50       /* Maximum number of reminders */
#define MSG_LEN 60          /* max length of reminders message */

int read_line(char str[], int n);

int main(void) {
    char reminders[MAX_REMIND][MSG_LEN+3];
    char day_str[3], msg_str[MSG_LEN+1];
    int day, i, j, num_remind = 0;

    for(;;) {
        if(num_remind == MAX_REMIND) {
            printf("--No space left--\n");
            break;
        }

        printf("Enter day and reminder: ");
        scanf("%2d", &day);
        if(day == 0)
            break;
        sprintf(day_str, "%2d", day);
        read_line(msg_str, MSG_LEN);

        for(i = 0; i < num_remind; i++)
            if(strcmp(day_str, reminders[i]) < 0)
                break;

        for(j = num_remind; j > i; j--) 
            strcpy(reminders[j], reminders[j - 1]);

        strcpy(reminders[i], day_str);
        strcat(reminders[i], msg_str);

        num_remind++;
    }

    printf("\nDay Reminder\n");
    for(i = 0; i < num_remind; i++)
        printf(" %s\n", reminders[i]);

    return 0;
}

int read_line(char str[], int n) {
    int ch, i = 0;
    while((ch = getchar()) != '\n')
        if (i < n)
            str[i++] = ch;

    str[i] =  '\0';
    return i;
}

What I understood is that, strings are stored in 2D array in which every row accepts string from the user. The program first takes date (in two decimal from the user) and convert it into string using sprintf() function. Then it compares the converted string date with the string stored in reminder[][] array.

I can't understand how it compares date with string. (It always return true in that case and breaks for statement at i = 0 everytime).

like image 291
Ashish Rawat Avatar asked Oct 22 '22 15:10

Ashish Rawat


2 Answers

strcmp used in this code is used for sorting. Add some debug code (after line 27) and you will see what result is produced by strcmp:

for(i = 0; i < num_remind; i++) {
    printf("%s comparing to %s is %d \n", day_str, reminders[i], strcmp(day_str, reminders[i]));
    if(strcmp(day_str, reminders[i]) < 0) {
            break;
    }
}

As you can see the for loop is interrupted when the new entered day_str is smaller than any other reminder from the beginning of the stored reminders. Obtained i in this way is used in the next for loop to shift all stored reminders from num_remind to i each by 1 place (from last element to i). At last these two lines are placing day_str and msg_str in the right place:

strcpy(reminders[i], day_str);
strcat(reminders[i], msg_str);

Take a look at this Insertion sort to understand this kind of sorting.

like image 108
Gregory Avatar answered Oct 28 '22 15:10

Gregory


The 'day' variable is an integer, which is composed into a string with the format "%2d", so the number 6 becomes " 6".

Then the program loops through the reminders, and breaks on this condition:

if(strcmp(day_str, reminders[i]) < 0)

strcmp is being used here to determine whether the string in day_str sorts earlier than the string in reminders[i].

The reminders[] array is therefore sorted by ascending days. This loop breaks at the insertion point for the new data. Then it moves all the existing reminders down one position in the array, making room to insert the new data.

like image 42
Paul Beckingham Avatar answered Oct 28 '22 16:10

Paul Beckingham