From the course: C Standard Library

String manipulation - C Tutorial

From the course: C Standard Library

Start my 1-month free trial

String manipulation

- [Instructor] String.h provides two very useful functions for copying a string from one location to another. The first one is called strycpy and it receives the destination string and the source string as parameters. It copies character after character in source into destination until the null termination character is found. You may notice that this is risky because if no null character is found in the allocated space for source, it will keep copying, possibly causing a segmentation fold. So that's where strncpy comes in. This function copies at most count characters from source to destination. Logically, the destination must have enough space to hold the intended amount of characters in both functions. Just like copying strings, string.h provides string concatenation functions. The first one is called strcat, which appends the source string at the end of the destination string. Again, this is done character by character until the end of source is found, if ever. So that's why there's strncat, which also specifies the number of characters to append in its count parameter.

Contents