All the string handling functions are prototyped in:
#include <string.h>
The common functions are described below:
char *stpcpy (const char *dest,const char *src)
-- Copy one string into another.
int strcmp(const char *string1,const char *string2)
-Compare string1 and string2 to determine alphabetic order.
char *strcpy(const char *string1,const char *string2)
-- Copy string2 to stringl.
char *strerror(int errnum)
-- Get error message corresponding to specified error number.
int strlen(const char *string)
-- Determine the length of a string.
char *strncat(const char *string1, char *string2, size_t n)
-- Append n characters from string2 to stringl.
int strncmp(const char *string1, char *string2, size_t n)
-- Compare first n characters of two strings.
char *strncpy(const char *string1,const char *string2, size_t n)
-- Copy first n characters of string2 to stringl.
int strcasecmp(const char *s1, const char *s2)
-- Case Insensitive versionStrcmp ().
int strncasecmp(const char *s1, const char *s2, int n)
-- Case Insensitive versionStrncmp ().
The use of most of the functions is straightforward, for example:
char *str1 = "HELLO";char *str2;int length;length = strlen("HELLO"); /* length = 5 */(void) strcpy(str2,str1);
Note that bothStrcat ()AndStrcopy ()Both return a copy of their first argument which is the destination array. Note the order of the arguments isDestination ArrayFollowedSource arrayWhich is sometimes easy to get the wrong around when programming.
TheStrcmp ()FunctionLexicallyCompares the two input strings and returns:
-
Less Than Zero
-
-- If String1Is lexically less String2
-
Zero
-
-- If String1And String2Are lexically equal
-
Greater than zero
-
-- If String1Is lexically greater String2
This can also confuse beginners and experience programmers forget this too.
TheStrncat (), strncmp ,()AndStrncpy ()Copy functions are string restricted version of their more general counterparts. They perform a similar task but only up to the firstNCharacters. NoteNullTerminated requirement may get violated when using these functions, for example:
char *str1 = "HELLO";char *str2;int length = 2;(void) strcpy(str2,str1, length); /* str2 = "HE" */
Str2Is not NULL terminated !! -- Beware
String searching
The Library also provides several string searching functions:
char *strchr(const char *string, int c)
-- Find first occurrence of characterCIn string.
char *strrchr(const char *string, int c)
-- Find last occurrence of characterCIn string.
char *strstr(const char *s1, const char *s2)
-- Locates the first occurrence of the stringS2In stringS1.
char *strpbrk(const char *s1, const char *s2)
-- Returns a pointer to the first occurrence in string S1 of any character from stringS2, Or a null pointer if no character fromS2Exists inS1
size_t strspn(const char *s1, const char *s2)
-- Returns the number of characters at the beginingS1That matchS2.
size_t strcspn(const char *s1, const char *s2)
-- Returns the number of characters at the beginingS1ThatDo notMatchS2.
char *strtok(char *s1, const char *s2)
-- Break the string pointed toS1Into a sequence of tokens, each of which is delimited by one or more characters from the string pointed toS2.
char *strtok_r(char *s1, const char *s2, char **lasts)
-- Has the same functionality as strtok () should t that a pointer to a string placeholder lasts must be supplied by the caller.
Strchr ()AndStrrchr ()Are the simplest to use, for example:
char *str1 = "Hello";char *ans;ans = strchr(str1,'l');
After this execution,AnsPoints to the locationStr1 + 2
Strpbrk ()Is a more general function that searches for the first occurrence of any of a group of characters, for example:
char *str1 = "Hello";char *ans;ans = strpbrk(str1,'aeiou');
Here,AnsPoints to the locationStr1 + 1, The location of the firstE.
Strstr () returns a pointer to the specified search string or a null pointer if the string is not found. if S2 points to a string with zero length (that is, the string ""), the function returns S1. for example,
char *str1 = "Hello";char *ans;ans = strstr(str1,'lo');
Will yieldAns=STR + 3.
Strtok ()Is a little more complicated in operation. If the first argument is not null then the function finds the position of any of the second argument characters. However, the position is remembered and any subsequent CALSStrtok ()Will start from this position if on these subsequent callthe first argument isNull. For example, if we wish to break up the stringStr1At each space and print each token on a new line we cocould do:
char *str1 = "Hello Big Boy";char *t1;for ( t1 = strtok(str1," "); t1 != NULL; t1 = strtok(NULL, " ") )printf("%s/n",t1);
Here we use the For Loop in a non-standard counting fashion:
- The initialisation CILSStrtok ()Loads the function with the stringStr1
- We terminate when T1 isNull
- We keep assigning tokensStr1ToT1Until termination by callingStrtok ()WithNullFirst argument.
Memory operations:<Memory. h> Finally we briefly overview some basic memory operations. Although not strictly string functions the functions are prototyped in#include <string.h>
:
void *memchr (void *s, int c, size_t n)
-- Search for a character in a buffer.
int memcmp (void *s1, void *s2, size_t n)
-- Compare two buffers.
void *memcpy (void *dest, void *src, size_t n)
-- Copy one buffer into another.
void *memmove (void *dest, void *src, size_t n)
-- Move a number of bytes from one buffer lo another.
void *memset (void *s, int c, size_t n)
-- Set all bytes of a buffer to a given character.
Their use is fairly straightforward and not dissimilar to comparable string operations (except t the exact length (N) Of the operations must be specified as there is no natural termination here ).
Note that in all caseBytesOf memory are copied.Sizeof ()Function comes in handy again here, for example:
char src[SIZE],dest[SIZE];int isrc[SIZE],idest[SIZE];memcpy(dest,src, SIZE); /* Copy chars (bytes) ok */memcpy(idest,isrc, SIZE*sizeof(int)); /* Copy arrays of ints */
Memmove ()Behaves in exactly the same wayMemcpy ()Cannot that the source and destination locations may overlap.
Memcmp ()Is similarStrcmp ()Snapshot T hereUnsigned bytesAre compared and returns Less Than Zero ifS1Is lessS2 Etc.