# A Little C Primer/C String Function Library

The string-function library requires the declaration:
The most important string functions are as follows:

## strlen

The "strlen" function gives the length of a string, not including the NUL character at the end:
This prints:

## strcpy

The "strcpy" function copies one string from another. For example:
This will print:
Please be aware of two features of this program:
* This program assumes that "s2" has enough space to store the final string. The "strcpy" function won't bother to check, and will give erroneous results if that is not the case.
* A string constant can be used as the source string instead of a string variable. Using a string constant for the destination, of course, makes no sense.
These comments are applicable to most of the other string functions.

## strncpy

There is a variant form of "strcpy" named "strncpy" that will copy "n" characters of the source string to the destination string, presuming there are that many characters available in the source string. For example, if the following change is made in the example program:
—then the results change to:
Notice that the parameter "n" is declared "size_t", which is defined in "string.h". Because there is no null byte among the first 5 characters, strncpy does not add '\0' after copying.

## strcat

The "strcat" function joins two strings:
This prints:

## strncat

There is a variant version of "strcat" named "strncat" that will append "n" characters of the source string to the destination string. If the example above used "strncat" with a length of 7:
—the result would be:
Again, the length parameter is of type "size_t".

## strcmp

The "strcmp" function compares two strings:
The "strcmp" function returns a 0 for a successful comparison, and nonzero otherwise. The comparison is case-sensitive, so answering "BLUE" or "Blue" won't work.
There are three alternate forms for "strcmp":

### strncmp

* A "strncmp" function which, as might be guessed, compares "n" characters in the source string with the destination string:

### stricmp

* A "stricmp" function that ignores case in comparisons.

### strnicmp

* A case-insensitive version of "strncmp" called "strnicmp".

## strchr

The "strchr" function finds the first occurrence of a character in a string. It returns a pointer to the character if it finds it, and null if not. For example:
This prints:
The character is defined as a character constant, which C regards as an "int". Notice how the example program increments the pointer before using it ("++p") so that it doesn't point to the ":" but to the character following it.

### strrchr

The "strrchr" function is almost the same as "strchr", except that it searches for the last occurrence of the character in the string.

## strstr

The "strstr" function is similar to "strchr" except that it searches for a string, instead of a character. It also returns a pointer:

## strlwr and strupr

The "strlwr" and "strupr" functions simply perform lowercase or uppercase conversion on the source string. For example:
—prints:
These two functions are only implemented in some compilers and are not part of ANSI C.

## Further reading

* C Programming/Strings
* C++ Programming/Code/IO/Streams/string