In C programming, string manipulation is essential for handling text-based data. C doesn't have a built-in string data type but instead uses character arrays, which are arrays of char terminated with a null character '\0'. To help manage strings, C provides a range of string functions in the <string.h> library. These functions allow you to perform common operations like copying, concatenating, comparing, and searching strings.
Let's explore the key string functions provided by the <string.h> library.
strlen() - Length of a StringThe strlen() function returns the number of characters in a string, excluding the null-terminating character '\0'.
size_t strlen(const char *str);
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of string: %zu\n", strlen(str)); // Output: 13
return 0;
}
strlen(str) returns 13, which is the number of characters in the string "Hello, World!", excluding the null terminator.strcpy() - Copying StringsThe strcpy() function copies the content of one string into another. The destination string must be large enough to hold the copied string and the null-terminator.
char *strcpy(char *dest, const char *src);
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
strcpy(dest, src); // Copies "Hello" into dest
printf("Destination string: %s\n", dest); // Output: Hello
return 0;
}
strcpy(dest, src) copies the string from src to dest, including the null-terminating character.strcat() - Concatenating StringsThe strcat() function appends the source string to the destination string, starting from the null character '\0' in the destination string.
char *strcat(char *dest, const char *src);
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = " World!";
strcat(str1, str2); // Concatenates " World!" to "Hello"
printf("Concatenated string: %s\n", str1); // Output: Hello World!
return 0;
}
strcat(str1, str2) appends the content of str2 to str1 and the result is "Hello World!".strcmp() - Comparing StringsThe strcmp() function compares two strings lexicographically (character by character).
0 if the strings are equal.int strcmp(const char *str1, const char *str2);
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result < 0) {
printf("'%s' is less than '%s'.\n", str1, str2);
} else {
printf("'%s' is greater than '%s'.\n", str1, str2);
}
return 0;
}
strcmp(str1, str2) compares "Apple" and "Banana", and since "Apple" is lexicographically less than "Banana", it returns a negative value.'Apple' is less than 'Banana'.
strchr() - Searching for a Character in a StringThe strchr() function searches for the first occurrence of a character in a string. It returns a pointer to the first occurrence of the character or NULL if the character is not found.
char *strchr(const char *str, int c);
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr = strchr(str, 'W'); // Find the first occurrence of 'W'
if (ptr != NULL) {
printf("Character found: %c at position: %ld\n", *ptr, ptr - str);
} else {
printf("Character not found.\n");
}
return 0;
}
strchr(str, 'W') finds the first occurrence of 'W' in the string "Hello, World!". It returns a pointer to that character, and the program prints its position.Character found: W at position: 7
strstr() - Searching for a SubstringThe strstr() function searches for the first occurrence of a substring in a string. It returns a pointer to the first character of the found substring or NULL if the substring is not found.
char *strstr(const char *haystack, const char *needle);
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *result = strstr(str, "World");
if (result != NULL) {
printf("Substring found at: %s\n", result);
} else {
printf("Substring not found.\n");
}
return 0;
}
strstr(str, "World") finds the first occurrence of the substring "World" in "Hello, World!" and returns a pointer to the beginning of the substring.Substring found at: World!
strtok() - Tokenizing a StringThe strtok() function is used to split a string into tokens based on delimiters. It modifies the original string by replacing delimiters with null characters.
char *strtok(char *str, const char *delim);
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World! Welcome to C.";
char *token = strtok(str, " ,.!"); // Delimiters: space, comma, dot, exclamation mark
while (token != NULL) {
printf("Token: %s\n", token);
token = strtok(NULL, " ,.!"); // Get the next token
}
return 0;
}
strtok(str, " ,.!") breaks the string "Hello, World! Welcome to C." into tokens separated by spaces, commas, dots, and exclamation marks.Token: Hello
Token: World
Token: Welcome
Token: to
Token: C
sprintf() - Formatting StringsThe sprintf() function formats data and stores it as a string. It works similarly to printf(), but instead of printing to the console, it stores the result in a string.
int sprintf(char *str, const char *format, ...);
#include <stdio.h>
int main() {
char str[50];
int age = 25;
sprintf(str, "I am %d years old.", age); // Format and store in str
printf("%s\n", str); // Output: I am 25 years old.
return 0;
}
sprintf(str, "I am %d years old.", age) stores the formatted string "I am 25 years old." in str.strlen(): Returns the length of a string.strcpy(): Copies one string to another.strcat(): Appends one string to another.strcmp(): Compares two strings.strchr(): Searches for a character in a string.strstr(): Searches for a substring in a string.strtok(): Tokenizes a string into smaller parts (tokens).sprintf(): Formats and stores a formatted string.The <string.h> library is a powerful tool for manipulating strings in C, and it provides essential functions for handling text efficiently. Understanding these functions will allow you to perform a wide variety of string operations in your programs.
Open this section to load past papers