C String Handling Functions
The C standard library provides string handling functions, mostly accessible through the header file. Without a C string data type, these functions are needed. Rather, strings are shown as character arrays that end with the null character (‘\0’). Common string manipulation, comparison, searching, and information retrieval activities are carried out by string handling routines on these null-terminated character arrays (or pointers to their initial character).
Strong features for jobs like creating editors, word processors, and other text-processing software are offered by the string handling library.
Among the frequently utilised string handling functions are:
strlen(): The length of a string is determined and returned by this method. It counts the amount of characters in the string up to the null character ‘\0’, which ends the string. The argument passed to the function is a pointer to a character array, or the string. For instance, strlen(“Peacock”) yields 7. Usually, its prototype is size_t strlen(const char *s);.
strcpy(): Copying the contents of one string into another (the destination) is done with this function. The function receives the source and destination strings’ base addresses, or pointers. The target string is referenced by the pointer it returns. A buffer overflow could result in unknown behaviour, hence it is imperative that the destination character array be big enough to accommodate the text, including the null terminator. The code char *strcpy(char *s1, const char *s2); is the prototype.
strcat(): A source string can be concatenated (joined or appended) to the end of a destination string using this function. It substitutes the null character from the first parameter with the string from the second argument. The destination array (the first argument), like strcpy, needs to be big enough to hold the length of both strings plus the null terminator. The destination string is sent back. The code char *strcat(char *s1, const char *s2); is its demo.
strcmp(): To compare two strings lexicographically, use this function. Character per character, it compares the two string parameters. An integer value is the function’s output:
- If the strings are same, then 0.
- a negative number in the event that the first string is smaller than the second.
- a positive number in the event that the first string is longer than the second. The code is int strcmp(const char *s1, s2);.
In , functions such as strncpy, strncmp, strchr, strstr, strtok, strrev, strlwr, and strupr are available. Many of these methods, including strcpy_s and strcat_s, were made optional “secure” variants in the C11 standard. These versions require extra parameters to define buffer widths and assist avoid buffer overflows, although not all compilers support them.
The reason these functions work with pointers is that strings in C are accessed by pointing to their initial character. An array of characters’ name serves as a constant pointer to the array’s first element.
Example: How to use a few common string handling functions:
#include <stdio.h> // Required for printf()
#include <string.h> // Required for string handling functions like strlen, strcpy, strcat, strcmp [1-10]
int main(void) {
// Declare and initialize a source string using a string literal.
// The compiler automatically adds the null terminator.
const char *source_string = "Hello, World!";
// Declare a character array to hold a copy of the string.
// It must be large enough for the source string + the null terminator.
// strlen(source_string) + 1 gives the required size.
char destination_string[56];
// Declare character arrays for concatenation.
// The first array needs space for the result.
char str1[56] = "Programming "; // Initial string for concatenation
char str2[] = "in C is fun!"; // String to append
// Declare strings for comparison.
const char *cmp_str1 = "apple";
const char *cmp_str2 = "banana";
const char *cmp_str3 = "apple";
// --- Using strlen ---
// Get the length of the source string (excluding null terminator).
size_t len = strlen(source_string); // strlen returns size_t
printf("Source string: \"%s\"\n", source_string); // %s prints a string
printf("Length of source string: %zu\n", len); // %zu is typically used for size_t
// --- Using strcpy ---
// Copy the source string to the destination array.
// strcpy returns a pointer to the destination, which printf can print.
printf("Before strcpy, destination_string might contain garbage.\n");
strcpy(destination_string, source_string); // Copies source_string into destination_string
printf("After strcpy, destination_string: \"%s\"\n", destination_string);
// --- Using strcat ---
// Concatenate str2 to the end of str1.
// Make sure str1 has enough space.
printf("str1 before strcat: \"%s\"\n", str1);
printf("str2: \"%s\"\n", str2);
strcat(str1, str2); // Appends str2 to str1
printf("After strcat, str1: \"%s\"\n", str1);
// --- Using strcmp ---
// Compare different strings.
// strcmp returns 0 if equal, <0 if first is less, >0 if first is greater.
int cmp_result1 = strcmp(cmp_str1, cmp_str2); // "apple" vs "banana"
int cmp_result2 = strcmp(cmp_str2, cmp_str1); // "banana" vs "apple"
int cmp_result3 = strcmp(cmp_str1, cmp_str3); // "apple" vs "apple"
printf("Comparing \"%s\" and \"%s\": %d\n", cmp_str1, cmp_str2, cmp_result1);
printf("Comparing \"%s\" and \"%s\": %d\n", cmp_str2, cmp_str1, cmp_result2);
printf("Comparing \"%s\" and \"%s\": %d\n", cmp_str1, cmp_str3, cmp_result3);
return 0;
}
Output:
Source string: "Hello, World!"
Length of source string: 13
Before strcpy, destination_string might contain garbage.
After strcpy, destination_string: "Hello, World!"
str1 before strcat: "Programming "
str2: "in C is fun!"
After strcat, str1: "Programming in C is fun!"
Comparing "apple" and "banana": -1
Comparing "banana" and "apple": 1
Comparing "apple" and "apple": 0
Using the underlying character arrays and the null terminator standard, this example shows how to use these functions to carry out basic operations on strings in C. To use pointer arithmetic and memory allocation efficiently and securely, keep in mind that comprehension is essential. One example of the close relationship between pointers and string manipulation in C is the copyString function, which we covered earlier in our chat . It is a manual implementation of string copying logic using pointers.