String Definition
A string in C is merely an array of characters. The length of a string is determined by a terminating null character: '\0'. So, a string with the contents, say, "abc" has four characters: 'a''b''c', and the terminating null ('\0') character.

The more commonly-used string functions

strcat & strncat: string concatenate

char *strcat(char* s1, const char* s2);
char *strncat(char *str1, const char *str2, size_t n)
size_t strlcat(char *dst, const char *src, size_t siz);

The strcat() function shall append a copy of the string pointed to by s2 (including the terminating null byte) to the end of the string pointed to by s1.

The initial byte of s2 overwrites the null byte at the end of s1. If copying takes place between objects that overlap, the behavior is undefined. The function returns s1.

The strncat() function shall append a copy of the first n byte of the string pointed to by s2 to the end of the string pointed to by s1.

The strlcat() appends src to string dst of size siz (unlike strncat, siz is the * full size of dst, not space left). At most siz-1 characters * will be copied. Always NUL terminates (unless siz <= strlen(dst)). * Returns strlen(src) + MIN(siz, strlen(initial dst)). * If retval >= siz, truncation occurred.

strchr: character search

char *strchr(const char *s, int c)  //search from beginning 
char *strrchr( const char *str, int ch ) //search from end

The strchr() function shall locate the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null byte is considered to be part of the string. The function returns the location of the found character, or a null pointer if the character was not found.

strcmp: string compare

int strcmp ( const char* s1, const char * s2 );
int strncmp ( const char* s1, const char * s2 , size_t n );

A rudimentary form of string comparison is done with the strcmp() function. It takes two strings as arguments and returns a value less than zero if the first is lexicographical less than the second, a value greater than zero if the first is lexicographical greater than the second, or zero if the two strings are equal. The comparison is done by comparing the coded (ascii) value of the characters, character by character.

The strncmp() is to compare string with size n.

This function is useful in comparisons, e.g.

if (strcmp(s, "whatever") == 0) /* do something */     ;

strcpy & strncpy: string copy

char *strcpy(char *restrict s1, const char *restrict s2);
char *strncpy(char *restrict s1, const char *restrict s2,size_t n);

The strcpy() function shall copy the C string pointed to by s2 (including the terminating null byte) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined. The function returns s1. There is no value used to indicate an error: if the arguments to strcpy() are correct, and the destination buffer is large enough, the function will never fail.

The strncpy() function shall copy not more than n bytes (bytes that follow a null byte are not copied) from the array pointed to by s2 to the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined. If the array pointed to by s2 is a string that is shorter than n bytes, null bytes shall be appended to the copy in the array pointed to by s1, until nbytes in all are written. The function shall return s1; no return value is reserved to indicate an error.

It is possible that the function will not return a null-terminated string, which happens if the s2 string is longer than n bytes.

strlen : string length

size_t strlen(const char *s);

The strlen() function shall compute the number of bytes in the string to which s points, not including the terminating null byte. It returns the number of bytes in the string. No value is used to indicate an error.

The less commonly-used string functions

strstr

char *strstr(const char *haystack, const char *needle);

The strstr() function shall locate the first occurrence in the string pointed to by haystack of the sequence of bytes (excluding the terminating null byte) in the string pointed to by needle. The function returns the pointer to the matching string in haystack or a null pointer if a match is not found. If needle is an empty string, the function returns haystack. 大海撈針

strtok: string token

Ref: 字串切割:STRTOK、STRTOK_R與STRSEP

char *strtok(char *restrict s1, const char *restrict delimiters); 
char *strtok_r(char *s, const char *delimiters, char **lasts); 

A sequence of calls to strtok() breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a byte from the string pointed to by delimiters.

The first call in the sequence has s1 as its first argument, and is followed by calls with a null pointer as their first argument. The separator string pointed to by delimiters may be different from call to call.

Each subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.

Because the strtok() function must save state between calls, and you could not have two tokenizers going at the same time.

The Single Unix Standard defined a similar function, strtok_r(), that does not need to save state.

The argument lasts points to a user-provided pointer which points to stored information necessary for strtok_r() to continue scanning the same string.

Leave a Reply

Close Menu