Exercise 10-7 - CBD page 355 - Lone Leth Thomsen

Solution index                   Textual C program


/** Solution to exercise 10.7, p. 355, "C by Dissection"
 *
 *  10. March, 2003. Lone Leth Thomsen
 **/


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


int mystrncmp(const char* s1, const char* s2, size_t n)              
        {
                int i;
                int ret = 0;
                for (i=0; i<n ;i++)                                         
                {
                        if ((s1[i]== NULL) && (s2[i] == NULL)) break;            
                        if (s1[i] == s2[i]) continue; else
                        {
                                if (s1[i] < s2[i]){ret = -1;break;} else {ret = 1;break;}   
                        };
                };
                return ret;                                                 
        };


int main(void)
{
        
        char* str1 = "aa";                                                
        char* str2 = "aaa";
        int size = 3;

        int i1;
        int i2;
        i1 = strncmp(str1,str2,size);                                      
        i2 = mystrncmp(str1,str2,size);                                    
        printf("%s %s %i %i %s",str1,str2,i1,i2,"\n");                    
        return 0;
}

int mystrncmp(const char* s1, const char* s2, size_t n)              
        {
                int i;
                int ret = 0;
                for (i=0; i<n ;i++)                                         
                {
                        if ((s1[i]== NULL) && (s2[i] == NULL)) break;            
                        if (s1[i] == s2[i]) continue; else
                        {
                                if (s1[i] < s2[i]){ret = -1;break;} else {ret = 1;break;}   
                        };
                };
                return ret;                                                 
        };
mystrncmp is supposed to work like the library function strncmp. mystrncmp takes two strings s1 and s2 and an integer n as arguments. It compares the first n characters of each string until we reach a null character in both strings at which point we break off since the two strings are both shorter than n. We return 0 because the two strings are equal. If the i'th character in each string are equal we look at the next character. Otherwise we check if the i'th character in s1 is less than the i'th character in s2 (exploiting that the null character has integer value 0). If this is the case we return -1. Otherwise we return 1.
 

int main(void)
{
        
        char* str1 = "aa";                                                
        char* str2 = "aaa";
        int size = 3;

        int i1;
        int i2;
        i1 = strncmp(str1,str2,size);                                      
        i2 = mystrncmp(str1,str2,size);                                    
        printf("%s %s %i %i %s",str1,str2,i1,i2,"\n");                    
        return 0;
}
str1, str2 and size in the main program are built-in test strings and number. Finally we call the library strncmp, then we call mystrncmp with the same argument. The results should be the same.
 


Generated: Wednesday, March 29, 2006, 12:33:06
This program dissection page is generated from an XML-in-LAML source file