๐ C String Handling – Mastering String Operations in C
In C, strings are arrays of characters, and string handling involves manipulating those arrays in various ways. This tutorial covers key string operations, including copying, concatenation, searching, and modifying strings, along with important considerations regarding memory management.
๐น String Declaration and Initialization
In C, a string is declared as a character array. The string is initialized with a set of characters followed by a null character \0
to mark the end of the string.
๐ Example 1: Declaring and Initializing a String
Here, we declare a string and initialize it:
#include <stdio.h> int main() { char str[] = "Hello, World!"; // Declaring and initializing a string printf("String: %s\n", str); return 0; }
๐น String Memory Allocation
In C, strings can also be dynamically allocated using malloc()
. This allows you to allocate memory for a string at runtime.
๐ Example 2: Dynamically Allocating Memory for a String
Here, we allocate memory for a string using malloc()
:
#include <stdio.h> #include <stdlib.h>> #include <string.h> int main() { char *str; str = (char *)malloc(20 * sizeof(char)); // Allocate memory for a string strcpy(str, "Dynamically Allocated!"); // Copying string to allocated memory printf("String: %s\n", str); free(str); // Free allocated memory return 0; }
๐น strcpy
– Copying Strings
The strcpy()
function copies the content of one string to another. The destination string must have enough memory to hold the copied content.
๐ Example 3: Using strcpy
to Copy a String
In this example, we copy the string using strcpy
:
#include <stdio.h> #include <string.h> int main() { char src[] = "Hello, World!"; char dest[20]; // Ensure sufficient space for the copied string strcpy(dest, src); // Copy string from src to dest printf("Destination String: %s\n", dest); return 0; }
๐น strcat
– Concatenating Strings
The strcat()
function concatenates (joins) two strings. It appends the second string to the end of the first string.
๐ Example 4: Using strcat
to Concatenate Strings
In this example, we concatenate two strings:
#include <stdio.h> #include <string.h> int main() { char str1[30] = "Hello"; char str2[] = " World!"; strcat(str1, str2); // Concatenate str2 to str1 printf("Concatenated String: %s\n", str1); return 0; }
๐น strlen
– Finding String Length
The strlen()
function returns the length of a string, excluding the null character \0
.
๐ Example 5: Using strlen
to Find String Length
This example shows how to use strlen
to find the length of a string:
#include <stdio.h> #include <string.h> int main() { char str[] = "C Programming!"; int length = strlen(str); // Get length of the string printf("Length of String: %d\n", length); return 0; }
๐น strcmp
– Comparing Strings
The strcmp()
function compares two strings lexicographically. It returns:
0
if the strings are equal,- 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 6: Using strcmp
to Compare Strings
Here, we compare two strings using strcmp
:
#include <stdio.h> #include <string.h> int main() { char str1[] = "C Programming"; char str2[] = "C Programming"; int result = strcmp(str1, str2); // Compare str1 and str2 if (result == 0) { printf("Strings are identical\n"); } else { printf("Strings are different\n"); } return 0; }
๐ฏ Key Points on C String Handling
- Strings are arrays of characters in C and require proper memory management.
strcpy()
copies one string to another,strcat()
concatenates strings, andstrlen()
returns the length of a string.- Memory for strings can be dynamically allocated using
malloc()
and should be freed usingfree()
when no longer needed.
๐ก Practice Challenge
Try modifying the examples and experiment with other string operations, such as reversing a string, searching for a substring, or converting string cases. Keep practicing to master string handling in C!