music

# string comparison

## summary

This subchapter looks at string comparison.

## free computer programming text book project

If you like the idea of this project,

# string comparison

This subchapter looks at string comparison.

## are two strings the same?

This example comes from a discussion on Google+ Programming Circle. The discusssion was about how to implement the standard C string fucntions without using the standard C library. Stéphane Zuckerman provided the following C code from an actual standard C library implementation.

Note that there are still two corrections needed to this routine, but the corrections are left to the student as an exercise.

int strcmp(const char* l, const char * r) {
while(*l && *l++ == *r++)
;
return (const unsigned char)l - *(const unsigned char)r;
}

The variables l and r are pointers to the beginning of the two strings (left and right). The official C standard uses the names str1 and str2.

The first short circuit test for the while loop is to determine if the current character pointed to by the pointer variable l equals zero or not. The standard C end of string marker is zero (all zero bits). We stop the test if the first string has ended. What happens if the second (r) string is longer? This code will claim that the two strings are equal. How do you fix this?

There is no corresponding test for the end of the second strng (r). The author of this code pointed out that “The beauty of this is also that you don’t need to test for both *l and *r in the while : either *l is done, and the comparison should stop; or it’s not, and you should continue comparing the strings — if *r is shorter than *l, then *r = '\0', which is necessarily different from the current value of *l”

The second part of the while test is to see if the strings have the same characters (equaiity) at each position of the strings (character arrays).

Notice that there is no work inside the while loop. All of the work is done by the while condition tests.

The standard for the official C routine is to return zero if the strings are equal, return a value greater than zero (positive number) if the first string is longer and return a dvalue less than zero (negative number) if the second string is longer. The normal case is to return one, zero, or negative one. Some implementations will return an integer that indicates not just which string is longer, but how much it is longer. This is a non-standard implementation and any software that depends on this extra information will break horribly when ported to another system.

There is an error in the return value of the sample code. Can you spot the error and correct it?

# free music player coding example

Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

View music player in action: www.musicinpublic.com/.

Create your own copy from the original source code/ (presented for learning programming).

## view text bookHTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

 previous page next page
 Tweets by @osdata

## free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

Some or all of the material on this web page appears in the

This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

†UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

Names and logos of various OSs are trademarks of their respective owners.