Welcome to the exciting world of C String Functions! In this comprehensive lesson, we'll dive deep into understanding, using, and applying various string functions in the C programming language. By the end of this lesson, you'll have a solid grasp of these essential tools, which will empower you to create more effective and efficient programs. Let's get started! š
<a name="introduction"></a>
C String Functions are a set of predefined functions in the C programming language that help us manipulate strings (arrays of characters). These functions are invaluable tools for any C programmer as they simplify string manipulation, making our code more readable, efficient, and error-free.
<a name="basic"></a>
The strlen() function is used to determine the length of a string. It returns the number of characters (excluding the null character \0) in the string.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int len = strlen(str);
printf("The length of the string is: %d\n", len);
return 0;
}š” Pro Tip: Always include the <string.h> header file to use string functions.
The strcpy() function copies the source string (src) to the destination string (dest). The destination string should have enough space to accommodate the source string, including the null character \0.
#include <stdio.h>
#include <string.h>
int main() {
char dest[20] = "";
char src[] = "CodeYourCraft";
strcpy(dest, src);
printf("The copied string is: %s\n", dest);
return 0;
}The strcat() function concatenates the source string (src) at the end of the destination string (dest). Both strings should have enough space to accommodate the concatenated string, including the null characters.
#include <stdio.h>
#include <string.h>
int main() {
char dest[20] = "Hello, ";
char src[] = "World!";
strcat(dest, src);
printf("The concatenated string is: %s\n", dest);
return 0;
}The strcmp() function compares two strings and returns an integer indicating their relationship. It returns 0 if the strings are equal, a positive value if the first string is lexicographically greater, and a negative value if the first string is lexicographically lesser.
#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;
}<a name="advanced"></a>
The strchr() function searches for the first occurrence of a specific character (c) in a string (str) and returns a pointer to the character if found, or a null pointer if not found.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char c = 'l';
char *result = strchr(str, c);
if (result != NULL) {
printf("The first occurrence of %c is at position: %d\n", c, result - str + 1);
} else {
printf("The character %c is not found in the string\n", c);
}
return 0;
}The strrchr() function searches for the last occurrence of a specific character (c) in a string (str) and returns a pointer to the character if found, or a null pointer if not found.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char c = 'l';
char *result = strrchr(str, c);
if (result != NULL) {
printf("The last occurrence of %c is at position: %d\n", c, result - str + 1);
} else {
printf("The character %c is not found in the string\n", c);
}
return 0;
}The strstr() function searches for the first occurrence of a substring (substr) within a string (str) and returns a pointer to the substring if found, or a null pointer if not found.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
char *result = strstr(str, substr);
if (result != NULL) {
printf("The substring %s is found at position: %d\n", substr, result - str + 1);
} else {
printf("The substring %s is not found in the string\n", substr);
}
return 0;
}The memset() function fills a block of memory with a specific value (val). It is useful for initializing arrays or strings with a specific value.
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "";
memset(str, '*', 10);
printf("%s\n", str);
return 0;
}The memcpy() function copies a block of memory from the source (src) to the destination (dest), with the size specified by n.
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "CodeYourCraft";
char dest[15] = "Craft";
memcpy(dest, src, 8);
printf("%s\n", dest);
return 0;
}The strcmpi() function is similar to strcmp(), but it compares two strings in a case-insensitive manner.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "APPLE";
char str2[] = "apple";
int result = strcmpi(str1, str2);
if (result == 0) {
printf("%s and %s are the same case-insensitively\n", str1, str2);
} else {
printf("%s and %s are different case-insensitively\n", str1, str2);
}
return 0;
}<a name="practical"></a>
#include <stdio.h>
#include <string.h>
void reverse(char* str) {
int len = strlen(str);
for (int i = 0; i < len / 2; ++i) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[] = "Hello, World!";
reverse(str);
printf("The reversed string is: %s\n", str);
return 0;
}#include <stdio.h>
#include <string.h>
int is_palindrome(char* str) {
int len = strlen(str);
for (int i = 0; i < len / 2; ++i) {
if (str[i] != str[len - i - 1]) {
return 0;
}
}
return 1;
}
int main() {
char str[] = "racecar";
if (is_palindrome(str)) {
printf("%s is a palindrome\n", str);
} else {
printf("%s is not a palindrome\n", str);
}
return 0;
}<a name="quiz"></a>
Which function is used to copy a source string to a destination string?
Which function is used to determine the length of a string?