Question: C language Implement the following specification: /* Compares str1 and str2 according to dictionary ( aka , lexicographic ) order , where characters

C language

Implement the following specification:

/* Compares str1 and str2 according to " dictionary" ( aka , " lexicographic ") order , where characters are ordered by their ASCII values . Returns -1 if str1 comes before str2 ; 0 if either str1 or str2 is NULL or if they are equal ; and 1 if str1 comes after str2 . */

int strcmp ( char * str1 , char * str2 );

For example, consider the following unit test in strcmp test.c:

# include < stdio .h >

// Write strcmp here .

int main () {

printf ( " aardvark , aardwolf %d\ n" , strcmp ( " aardvark " , " aardwolf " ));

printf ( " AVAST , avast %d\ n" , strcmp (" AVAST " , " avast " ));

printf ( " ahoy , ahoy % d " , strcmp ( " ahoy " , " ahoy " ));

printf ( " Watch for aardvarks !, " " Watches aren t for aardwolves. % d " ,

strcmp ( " Watch for aardvarks !" , " Watches aren t for aardwolves." ));

printf ( " zoology , zoo %d\ n" , strcmp (" zoology " , " zoo " ));

return 0;

}

Once strcmp is added, compiling and running indicates the ASCII-based dictionary order of these strings:

$ gcc -Wall -Wextra -o strcmp_test strcmp_test.c

$ ./strcmp_test

aardvark, aardwolf -1

AVAST, avast -1

ahoy, ahoy 0

Watch for aardvarks!, Watches arent for aardwolves. -1

zoology, zoo 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!