Predefining Cstring Functions
-
In addition to strcpy and strcmp, the header file cstring contains a few more commonly used functions.
-
The function strlen returns an integer equal to the length of the cstring in its argument, not counting the null character.
-
The function strcat(TargetString, SrcString) concatenates (joins together) the cstring value SrcString onto the end of the cstring TargetString. The first argument must be a cstring and the second argument can be anything that evaluates to a cstring value, such as a quoted string.
-
Here is an example:
char string1[50] = "Have you ever seen a man";
strcat(string1, "eating chicken?");
This code will change the value of string1 to:
"Have you ever seen a maneating chicken?"
Note that the words "man" and "eating" are concatenated with no space between them.
This careless usage of the strcat function has turned an ordinary question into a more interesting one.
-
The danger associated with the function strcpy and strcat is that most C++ compiler let you assign a string that is too long for the cstring variable receiving the value.
-
For example the following code may not produce an error message:
char shortString[8];
strcpy(shortString, "Here Comes The Sun!");
char string1[ ] = "Problem,";
strcat(string1, " for sure!");