Previous | Table of Contents | Next |
3.10.2.3. Comparing Strings
strcmp
int strcmp(const char *s1, const char *s2)
strcmp compares the strings s1 and s2. It returns the value 0 if the two strings compare equally, a negative value if s1 is lexicographically less than s2, or a positive value if s1 is greater than s2. The lexicographical comparison is made on a character-by-character basis using the machines character set values.
Here is an implementation of strcmp. (Notice that it uses a for loop without an initialization expression.)
int strcmp(const char *s1, const char *s2) { for(; *s1 == *s2; s1++, s2++) { if(*s1 == \0) return 0; } return *s1 - *s2; }
The first thing to check is whether corresponding characters are equal; if they remain equal up to and including the terminating \0, both strings are the same. Otherwise, a simple way to obtain the ordering between the two strings is to subtract the values of the mismatched characters (which also works if one string is an initial substring of the other because the value of the character \0 is, by definition, 0). In the presence of negative character values, however (which might occur if type char is signed), better results would be achieved by forcing unsigned interpretation. One way to do this (at the cost of explicit casts) would be to replace the last line by
return *(unsigned char *)s1 - *(unsigned char *)s2;
strncmp
int strncmp(const char *s1, const char *s2, size_t n)
strncmp compares up to n characters from the strings s1 and s2. If the strings compare equal in their first n characters, they are considered equal even if no \0 character is found. The comparison is otherwise the same as for strcmp.
strcoll
int strcoll(const char *s1, const char *s2)
strcoll compares the strings s1 and s2, much like strcmp, except that strcoll performs a locale-dependent comparison (perhaps taking into account multinational character sets) as determined by the current LC_COLLATE category (see section 3.10.9).
strxfrm
size_t strxfrm(char *dest, const char *src, size_t n)
strxfrm creates a modified copy of the string pointed to by src in the destination buffer dest. The modified string is terminated with a \0 character, but no more than n characters (including the \0) are written to dest. The modification is such that if two strings, each converted by strxfrm, are then compared using strcmp, the same ordering will be determined as if the two original strings had been compared using strcoll, using appropriate locale-specific comparisons. strxfrm returns the number of characters written to dest, not including the terminating \0.
3.10.2.4. Searching for Characters and Strings
strchr, strrchr
char *strchr(const char *s, int c) char *strrchr(const char *s, int c)
These functions search the strings pointed to by s for a character with the value c. strchr returns a pointer to the first such character; strrchr returns a pointer to the last (rightmost) such character. Both functions return a null pointer if the character is not found.
strstr
char *strstr(const char *s, const char *pat)
strstr searches the string pointed to by s for the first instance of the substring pat, and returns either a pointer to that substring or a null pointer if the pattern is not found.
strspn, strcspn
size_t strspn(const char *s, const char *set) size_t strcspn(const char *s, const char *set)
These functions compute the length of the initial portion of the strings s that are composed entirely of characters found in or not found in the given string set. strspn returns the initial number of characters all of which are present in set, and strcspn returns the initial number of characters none of which are present in set.
strpbrk
char *strpbrk(const char *s, const char *set)
strpbrk searches the string pointed to by s for any character occurring within set, returning a pointer to the first one found or a null pointer if none are found.
3.10.2.5. Operations on Blocks of Characters
memcpy, memmove
void *memcpy(void *dest, const void *src, size_t n) void *memmove(void *dest, const void *src, size_t n)
These functions copy exactly n bytes from the blocks pointed to by src to the destinations dest, returning dest. The text copied is not treated as a string; \0 characters are not searched for or treated specially. If the source and destination blocks overlap, memmove is careful to perform the copying appropriately (not overwriting characters until they have been copied); memcpy offers no such guarantee.
memcmp
int memcmp(const void *p1, const void *p2, size_t n)
memcmp compares exactly n characters from the blocks pointed to by p1 and p2. The blocks of characters are not treated as strings; \0 characters are not searched for or treated specially. The comparison is otherwise the same as for strcmp (see section 3.10.2.3).
Previous | Table of Contents | Next |