๐ C String Functions – Manipulating Strings in C
C provides several built-in functions to work with strings. These functions allow you to manipulate strings easily by performing tasks like measuring string length, copying strings, concatenating them, and comparing them. In this tutorial, we will cover some of the most commonly used string functions in C.
๐น strlen – Find the Length of a String
The strlen() function is used to calculate the length of a string. It counts the number of characters before the null terminator \0.
๐ Example 1: Using strlen to Get String Length
In this example, we find the length of a string using strlen:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str); // Get the length of the string
printf("Length of the string: %d\n", length);
return 0;
}
๐น strcpy – Copy a String
The strcpy() function is used to copy one string into another. The destination string must be large enough to hold the copied string, including the null terminator \0.
๐ Example 2: Using strcpy to Copy a String
This example demonstrates how to use strcpy to copy a string:
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello";
char destination[10]; // Ensure sufficient space for the copied string
strcpy(destination, source); // Copy string from source to destination
printf("Copied string: %s\n", destination);
return 0;
}
๐น strcat – Concatenate Two Strings
The strcat() function is used to concatenate (join) two strings. It appends the second string to the first string, overwriting the null terminator of the first string.
๐ Example 3: Using strcat to Concatenate Strings
In this example, we concatenate two strings:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = " World!";
strcat(str1, str2); // Concatenate str2 to str1
printf("Concatenated string: %s\n", str1);
return 0;
}
๐น strcmp – Compare Two Strings
The strcmp() function compares two strings lexicographically. It returns:
0if the strings are identical,- a negative value if the first string is lexicographically less than the second,
- a positive value if the first string is lexicographically greater than the second.
๐ Example 4: Using strcmp to Compare Strings
In this example, we compare two strings:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2); // Compare the two strings
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
๐ฏ Key Points about C String Functions
strlen- Returns the length of a string (excluding\0).strcpy- Copies one string to another.strcat- Concatenates two strings.strcmp- Compares two strings.
๐ก Practice Challenge
Try experimenting with different string functions. Modify the examples to perform other operations like reversing a string, searching for a substring, or converting case. Practice these functions and see how they work!