![]() |
Saturday, September 23, 2023 |
By The Voodooman | faq | about |
C-Help IndexAssigning/Copying Finding Length Comparing Appending/Joining Cloning Other Functions |
String FunctionsIncludes#include <string.h>The standard string functions that are available to a C programmer may seem a little strange if you are coming from a background of another language such as Visual Basic or Pascal. In particular, a lot of VB programmers have difficulty understanding why you should use strcpy to assign a value to a string variable, instead of assigning it directly (as in foo="the string";). However, it should not take you too long to understand the various C functions that are used to manipulate strings. Assigning/CopyingIn C, the process of assigning a string to a string variable consists of copying the value into a variable using a function. You cannot assign a string directly to a variable, since a string is an array. Hence you cannot do something like foo = "stringvalue";. Instead you must use a function to do your assignment. There are two functions that can be used for copying strings from the source to the destination. The thing to remember is to ALWAYS ensure that your destination array is large enough.
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRCPY(3) Linux Programmer's Manual STRCPY(3)
NAMEstrcpy, strncpy - copy a string SYNOPSIS#include <string.h> DESCRIPTIONThe strcpy() function copies the string pointed to be src (including the terminating `\0' character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. RETURN VALUEThe strcpy() and strncpy() functions return a pointer to the destination string dest. BUGSIf the destination string of a strcpy() is not large enough (that is, if the programmer was stupid/lazy, and failed to check the size before copying) then anything might happen. Overflowing fixed length strings is a favourite cracker technique. CONFORMING TOSVID 3, POSIX, BSD 4.3, ISO 9899 SAMPLE CODE
TOPFindingThere are three functions that are usually used for locating characters or substrings within strings. The functions are strchr, strrchr and strstr. The purpose of these three functions is as follows:
There are also two other functions called index and rindex, whose syntax is virtually identical to strchr and strrchr respectively. These two function will therefore not be discussed here. Another function that is sometimes used is called strpbrk. This function works like strchr, except that it can be given multiple characters to search for. The documention for this function is presented here without any working code. Note that the code for this section is in two parts: The first one appears below the documentation for strchr and strpbrk and the second one appears below the documentation for strstr. $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRCHR(3) Linux Programmer's Manual STRCHR(3)
NAMEstrchr, strrchr - locate character in string SYNOPSIS#include <string.h> DESCRIPTIONThe strchr() function returns a pointer to the first occurrence of the character c in the string s. RETURN VALUEThe strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found. CONFORMING TOSVID 3, POSIX, BSD 4.3, ISO 9899 $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRPBRK(3) Linux Programmer's Manual STRPBRK(3)
NAMEstrpbrk - search a string for any of a set of characters SYNOPSIS#include <string.h> DESCRIPTIONThe strpbrk() function locates the first occurrence in the string s of any of the characters in the string accept. RETURN VALUEThe strpbrk() function returns a pointer to the character in s that matches one of the characters in accept, or NULL if no such character is found. CONFORMING TOSVID 3, POSIX, BSD 4.3, ISO 9899 SAMPLE CODE
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRSTR(3) Linux Programmer's Manual STRSTR(3)
NAMEstrstr - locate a substring SYNOPSIS#include <string.h> DESCRIPTIONThe strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating `\0' characters are not compared. RETURN VALUEThe strstr() function returns a pointer to the beginning of the substring, or NULL if the substring is not found. BUGSEarly versions of Linux libc (like 4.5.26) would not allow an empty argument. Later versions (like 4.6.27) work cor rectly, and return haystack when needle is empty. SAMPLE CODE
TOPLengthThe function to find the length of a given string is called strlen. The function returns the length of the string, not the length of the array that contains it. Remember that if the string contains any null ('\0') characters in it, then the function returns the length upto the first null character alone.$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRLEN(3) Linux Programmer's Manual STRLEN(3)
NAMEstrlen - calculate the length of a string SYNOPSIS#include <string.h> DESCRIPTIONThe strlen() function calculates the length of the string s, not including the terminating `\0' character. RETURN VALUEThe strlen() function returns the number of characters in s. CONFORMING TOSVID 3, POSIX, BSD 4.3, ISO 9899 SAMPLE CODE
TOPComparingThere are four functions that are used to compare strings. Two are case-sensitive and two are not. The functions are:
Note that these functions all return a value that may be less than, equal to or greater than 0. If the value returned is less than zero, then the first string is less than the second string. If the value returned is greater than zero, then the first string is greater than the second string. If the value returned is zero, then the two strings are equal. There is also a fifth function called strcoll which compares strings according to locale settings. The arguments for this function are identical to strcmp. $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRCMP(3) Linux Programmer's Manual STRCMP(3)
NAMEstrcmp, strncmp - compare two strings SYNOPSIS#include <string.h> DESCRIPTIONThe strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. RETURN VALUEThe strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. CONFORMING TOSVID 3, POSIX, BSD 4.3, ISO 9899 $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRCASECMP(3) Linux Programmer's Manual STRCASECMP(3)
NAMEstrcasecmp, strncasecmp - compare two strings ignoring case SYNOPSIS#include <string.h> DESCRIPTIONThe strcasecmp() function compares the two strings s1 and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. RETURN VALUEThe strcasecmp() and strncasecmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. CONFORMING TOBSD 4.4 SAMPLE CODE
TOPAppending/JoiningThere are two functions that are used to append/join strings together:You can also use the sprintf function to concatenate various types into a string variable. The author personally uses sprintf as a personal preference. $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRCAT(3) Linux Programmer's Manual STRCAT(3)
NAMEstrcat, strncat - concatenate two strings SYNOPSIS#include <string.h> DESCRIPTIONThe strcat() function appends the src string to the dest string overwriting the `\0' character at the end of dest, and then adds a terminating `\0' character. The strings may not overlap, and the dest string must have enough space for the result. RETURN VALUEThe strcat() and strncat() functions return a pointer to the resulting string dest. CONFORMING TOSVID 3, POSIX, BSD 4.3, ISO 9899 SAMPLE CODE
TOPCloningSometimes, it is necessary to clone the contents of a string, so that the copy can be manipulated without affecting the original string. Hence, it is necesssary to quickly copy a string without too much fuss. You can always copy the string using the strcmp function, but there is also another method, i.e. use the function strdup. Note that strdup allocates memory dynamically for the copy and you must free this memory using the free function, when you are done with the copy.$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; ?>
STRDUP(3) Linux Programmer's Manual STRDUP(3)
NAMEstrdup - duplicate a string SYNOPSIS#include <string.h> DESCRIPTIONThe strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3). RETURN VALUEThe strdup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available. ERRORSENOMEM Insufficient memory available to allocate duplicate string. CONFORMING TOSVID 3, BSD 4.3 SAMPLE CODE
TOPOther FunctionsThere are some other string functions, which are not as frequently used as the ones above. You can determine the usage of these functions by reading the man pages.
In addition, Linux provides another function called strfry, whose purpose is to create anagrams of a string. Why this would be useful is beyond me, but it's there and so I'm mentioning it. This function is unique to Linux C Library and the GNU C Library alone. |