Comparing Strings with strcmp and strncmp

Figure 20.24 compares three strings using strcmp (lines 15–17) and strncmp (lines 20–22). Function strcmp compares its first string argument with its second string argument character by character. The function returns zero if the strings are equal, a negative value if the first string is less than the second string and a positive value if the first string is greater than the second string. Function strncmp is equivalent to strcmp, except that strncmp compares up to a specified number of characters. Function strncmp stops comparing characters if it reaches the null character in one of its string arguments. The program prints the integer value returned by each function call.


 1   // Fig. 20.24: fig20_24.cpp
 2   // Using strcmp and strncmp.
 3   #include <iostream>
 4   #include <iomanip>
 5   #include <cstring> // prototypes for strcmp and strncmp
 6   using namespace std;
 7
 8   int main()
 9   {
10      const char *s1 = "Happy New Year";
11      const char *s2 = "Happy New Year";
12      const char *s3 = "Happy Holidays";
13
14      cout << "s1 = " << s1 << " s2 = " << s2 << " s3 = " << s3
15         << " strcmp(s1, s2) = " << setw( 2 ) << strcmp( s1, s2 )
16         << " strcmp(s1, s3) = " << setw( 2 ) << strcmp( s1, s3 )
17         << " strcmp(s3, s1) = " << setw( 2 ) << strcmp( s3, s1 );
18
19      cout << " strncmp(s1, s3, 6) = " << setw( 2 )
20         << strncmp( s1, s3, 6 ) << " strncmp(s1, s3, 7) = " << setw( 2 )
21         << strncmp( s1, s3, 7 ) << " strncmp(s3, s1, 7) = " << setw( 2 )
22         << strncmp( s3, s1, 7 ) << endl;
23   } // end main


s1 = Happy New Year
s2 = Happy New Year
s3 = Happy Holidays

strcmp(s1, s2) =  0
strcmp(s1, s3) =  1
strcmp(s3, s1) = -1

strncmp(s1, s3, 6) =  0
strncmp(s1, s3, 7) =  1
strncmp(s3, s1, 7) = -1


Fig. 20.24. strcmp and strncmp.


Image Common Programming Error 20.9

Assuming that strcmp and strncmp return one (a true value) when their arguments are equal is a logic error. Both functions return zero (C++’s false value) for equality. Therefore, when testing two strings for equality, the result of the strcmp or strncmp function should be compared with zero to determine whether the strings are equal.


To understand what it means for one string to be “greater than” or “less than” another, consider the process of alphabetizing last names. You’d, no doubt, place “Jones” before “Smith,” because the first letter of “Jones” comes before the first letter of “Smith” in the alphabet. But the alphabet is more than just a list of 26 letters—it’s an ordered list of characters. Each letter occurs in a specific position within the list. “Z” is more than just a letter of the alphabet; “Z” is specifically the 26th letter of the alphabet.

How does the computer know that one letter “comes before” another? All characters are represented inside the computer as numeric codes; when the computer compares two strings, it actually compares the numeric codes of the characters in the strings.

[Note: With some compilers, functions strcmp and strncmp always return -1, 0 or 1, as in the sample output of Fig. 20.24. With other compilers, these functions return 0 or the difference between the numeric codes of the first characters that differ in the strings being compared. For example, when s1 and s3 are compared, the first characters that differ between them are the first character of the second word in each string—N (numeric code 78) in s1 and H (numeric code 72) in s3, respectively. In this case, the return value will be 6 (or -6 if s3 is compared to s1).]

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.46.58