Welcome to our deep dive into C String Algorithms! In this comprehensive guide, we'll explore the essential string functions that every C programmer should know. Let's embark on this exciting journey together! 🚀
<a name="intro"></a>
In C programming, a string is an array of characters with the first element representing the initial character, and the null character ('\0') denoting the end of the string. Let's get familiar with some key terms:
<a name="basic"></a>
Here, we'll cover basic string functions such as strlen(), strcmp(), and strcpy().
strlen() Function 📝strlen() is a built-in C function that calculates the length of a string.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int len = strlen(str);
printf("String Length: %d\n", len);
return 0;
}In this example, the strlen(str) function calculates the length of the string str and stores the result in the len variable. The output will be 13.
strcmp() Function 📝strcmp() is a built-in C function that compares two strings lexicographically (character by character).
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
int result = strcmp(str1, str2);
if (result < 0) {
printf("%s comes before %s\n", str1, str2);
} else if (result > 0) {
printf("%s comes after %s\n", str1, str2);
} else {
printf("%s is equal to %s\n", str1, str2);
}
return 0;
}In this example, the strcmp(str1, str2) function compares the two strings str1 and str2. If the result is less than 0, str1 comes before str2. If the result is greater than 0, str1 comes after str2. If the result is 0, the strings are equal.
strcpy() Function 📝strcpy() is a built-in C function that copies the source string into the destination string.
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Source String";
char dest[20];
strcpy(dest, src);
printf("Destination String: %s\n", dest);
return 0;
}In this example, the strcpy(dest, src) function copies the src string into the dest array.
<a name="advanced"></a>
Here, we'll cover advanced string functions such as strcat(), strncat(), and strchr().
strcat() Function 📝strcat() is a built-in C function that concatenates two strings.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, ";
char str2[] = "World!";
char result[30];
strcat(result, str1);
strcat(result, str2);
printf("Result: %s\n", result);
return 0;
}In this example, the strcat(result, str1) function appends the str1 string to the result array, and then the strcat(result, str2) function appends the str2 string to the updated result array.
strncat() Function 📝strncat() is a built-in C function that concatenates a limited number of characters from the source string to the destination string.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, ";
char str2[] = "World!";
char result[30];
strncat(result, str1, 5);
strncat(result, str2, 6);
result[13] = '\0'; // Adding null character to avoid string corruption
printf("Result: %s\n", result);
return 0;
}In this example, the strncat(result, str1, 5) function appends the first 5 characters of the str1 string to the result array, and then the strncat(result, str2, 6) function appends the first 6 characters of the str2 string to the updated result array.
strchr() Function 📝strchr() is a built-in C function that searches a string for a specified character.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char* result = strchr(str, 'o');
if (result != NULL) {
printf("Character 'o' found at position: %d\n", result - str + 1);
} else {
printf("Character 'o' not found in the string.\n");
}
return 0;
}In this example, the strchr(str, 'o') function searches the str string for the character 'o'. If the character is found, the function returns a pointer to the first occurrence of the character.
<a name="quiz"></a>
:::quiz Question: What is the output of the following code snippet?
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
char str3[] = "Apple Banana";
printf("strcmp(str1, str2): %d\n", strcmp(str1, str2));
printf("strcmp(str1, str3): %d\n", strcmp(str1, str3));
return 0;
}A: 1, 1
B: -1, 0
C: 0, -1
Correct: B
Explanation: The strcmp(str1, str2) function compares Apple and Banana, which results in a negative number (-1), indicating that Apple comes before Banana lexicographically. The strcmp(str1, str3) function compares Apple and Apple Banana, which results in 0, indicating that they are equal.