Beginning Programming with C For Dummies by Gookin Dan

Beginning Programming with C For Dummies by Gookin Dan

Author:Gookin, Dan [Gookin, Dan]
Language: eng
Format: epub, mobi, pdf
Publisher: Wiley
Published: 2013-10-03T14:00:00+00:00


strlen()

Returns the length of a string, not counting the \0 or NULL character at the end of the string.

More string functions are available than are shown in Table 13-3. Many of them do specific things that require a deeper understanding of C. The ones shown in the table are the most common.

All the string functions in Table 13-3 require the string.h header file to be included with your source code:

#include <string.h>

On a Unix system, you can review all the string functions by typing the command man string in a terminal window.

Comparing text

Strings are compared by using the strcmp() function and all its cousins: strncmp(), strcasecmp(), and strncasecmp().

The string-comparison functions return an int value based on the result of the comparison: 0 for when the strings are equal, or a higher or lower int value based on whether the first string's value is greater than (higher in the alphabet) or less than (lower in the alphabet) the second string. Most of the time, you just check for 0.

Listing 13-3 uses the strcmp() function in Line 13 to compare the initialized string password with whatever text is read at Line 11, which is stored in the input array. The result of that operation is stored in the match variable, which is used in an if-else decision tree at Line 14 to display the results.

Listing 13-3: Let Me In

#include <stdio.h>

#include <string.h>



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.