String functions in C
Archive - Originally posted on "The Horse's Mouth" - 2010-06-30 09:51:48 - Graham EllisA String is a NUL terminated array of chars. Remember to allow space in the string for the NUL, and for any new line character you may have too, if you dimension it manually. And remember to add a NUL back on to the end if you extend a string by adding individual characters to it.
There are lots of standard C function for handling strings - here's a categorised list of the more basic ones:
* Searching
index find next occurence
rindex find previous occurrence of
strchr find character in a string (first occurrence)
strrchr find character in a string (last occurrence)
strpbrk find one string in another and return a pointer
strstr locate one string in another
* Comparing
strcasecmp string compare ignore case
strcmp string compare
strncmp string compare - first "n" characters
strncasecmp compare first "n" characters of 2 strings, ignoring case
* Copying
stpcpy string copy (return a pointer)
strcat join strings (concatenate)
strcpy copy string
strncpy copy first "n" characters of a string
strncat join up to "n" character of one string to another
* Scanning
strcspn how many characters in a string are not within a specified set?
strspn how many characters in a string are within a specified set?
strlen how long is a string?
strsep find a separator and return a pointer beyond it
strtok string tokeniser (you should now use strsep)
* Regular expressions - regex.h
regcomp compile a regular expression then ...
regexec ... match a regular expression and ...
regfree ... free up any internal memory used.
regerror handle regular expression error messages
Odd characters in the function name? Remember ...
c - compliment (not)
p - pointer
n - number / count
r - reverse (from end)