- strcat() - concatenate two strings.
- strcpy() - copy a string (handy for doing string assignments).
- strcmp() - compare two strings.
- strlen() - return the length of a string- this does not include the terminating zero.
- strchr() - find a character in a string searching from left.
- strncat().
- strncpy().
- strncmp().
Memory Management
You are responsible for making sure that strings are properly allocated and freed. For instance thisstring a="A very Long String";While it looks ok is a guaranteed disaster. The first line executes fine but the second takes exception. The strcat() function does not allocate any memory. The compiler would perceive this text as being read-only so it's probably located somewhere that cannot be written to. In this case you must allocate enough space to store the combined strings.
string b=strcat(a," and now even longer!") ;
Example 4 demonstrates this correctly:
#include "stdlib.h"
#include <stdio.h>
#include <string.h>
typedef char * string;
int main(int argc, char* argv[])
{
string a="A very Long String";
string b=" and now even longer!";
string c=malloc(strlen(a) + strlen(b)+1) ; /* +1 DFTTZ! */
strcpy(c,a) ;
strcat(c,b) ;
printf("c = %s",c) ;
free(c) ;
return 0;
}
On the next page - More About This Example

