In C, strings are essentially arrays of characters terminated by a null character (�
). This null character indicates the end of the string.
Declaring and Initializing Strings
There are two common ways to declare and initialize strings in C:
-
Using character arrays:
char str[10] = "hello";
This creates a character array
str
with a size of 10 elements and initializes it with the string “hello”. The null character (�
) is automatically added at the end -
Using string literals:
char *str = "hello";
This creates a pointer
str
that points to a read-only memory location containing the string “hello”. You cannot modify the contents of a string literal.
Accessing Characters in a String
You can access individual characters in a string using array indexing:
char str[] = "hello";
printf("%cn", str[0]); // Output: h
String Copying Functions
strcpy(dest, src): Copies the string src to dest.
strncpy(dest, src, n): Copies at most n characters from src to dest.
strlcpy(dest, src, size): Copies at most size – 1 characters from src to dest, ensuring null termination.
String Concatenation Functions
strcat(dest, src): Concatenates src to the end of dest.
strncat(dest, src, n): Concatenates at most n characters from src to the end of dest.
strlcat(dest, src, size): Concatenates at most size – 1 characters from src to the end of dest, ensuring null termination.
String Comparison Functions
strcmp(str1, str2): Compares str1 and str2 lexicographically. Returns 0 if equal, negative if str1 is less, positive if str1 is greater.
strncmp(str1, str2, n): Compares the first n characters of str1 and str2.
strcasecmp(str1, str2): Compares str1 and str2 lexicographically, ignoring case.
strncasecmp(str1, str2, n): Compares the first n characters of str1 and str2, ignoring case.
String Search Functions
strstr(haystack, needle): Finds the first occurrence of needle within haystack.
strnstr(haystack, needle, len): Finds the first n characters of needle within haystack.
strchr(str, c): Finds the first occurrence of character c in str.
strrchr(str, c): Finds the last occurrence of character c in str.
String Length and Modification Functions
strlen(str): Returns the length of str (excluding the null terminator).
strrev(str): Reverses the string str in place.
strtoupper(str): Converts all characters in str to uppercase.
strlower(str): Converts all characters in str to lowercase.
Other String Functions
memset(str, c, n): Sets the first n characters of str to c.
memcpy(dest, src, n): Copies n bytes from src to dest.
memmove(dest, src, n): Copies n bytes from src to dest, handling overlapping memory regions.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = " world";
// Concatenation
strcat(str1, str2);
printf("Concatenated string: %sn", str1);
// Comparison
if (strcmp(str1, "hello world") == 0) {
printf("Strings are equaln");
}
// Search
char *ptr = strstr(str1, "world");
if (ptr != NULL) {
printf("Found "world" at index %ldn", ptr - str1);
}
return 0;
}