In C, strings are represented as arrays of characters terminated by a null character ('\0'). This null character signifies the end of the string, allowing functions to determine where the string ends. Each character in the string occupies one byte of memory, and the null character ensures that operations on the string do not read beyond its intended end.
Strings in C can be declared in two primary ways:
You can declare a string using a character array. The size of the array must be large enough to hold the string and the null terminator.
char str[10]; // Declares a character array of size 10Here, str is automatically sized to 6 to accommodate the 5 characters and the null terminator.
In this case, str can hold up to 9 characters plus the null terminator. You can initialize it as follows:
char str[10] = "Hello"; // 'Hello' is 5 characters + 1 null terminator
A string literal is a sequence of characters enclosed in double quotes. When you assign a string literal to a character array, the compiler automatically adds the null terminator.
char str[] = "Hello"; // The size of the array is automatically set to 6
You can access and modify individual characters in a string using array indexing. Remember that string indices start at 0.
Traversing a string involves accessing each character in the string one by one. In C, strings are arrays of characters ending with a null character ('\0'). You can traverse a string using either of the following methods:
To traverse a string using its length, you first need to determine the length of the string, which can be done using the strlen() function from the <string.h> library. This method is useful when you need to process a string of known length.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str); // Get the length of the string
for (int i = 0; i < length; i++) {
printf("%c ", str[i]); // Print each character
}
printf("\\n");
return 0;
}
H e l l o , W o r l d !
You can also traverse a string until you encounter the null character ('\0'). This method is useful because it does not require knowing the string length beforehand, making it more flexible for strings of unknown or variable lengths.
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
for (int i = 0; str[i] != '\0'; i++) {
printf("%c ", str[i]); // Print each character
}
printf("\\n");
return 0;
}
H e l l o , W o r l d !
C provides several standard library functions for string manipulation defined in the <string.h> header:
| Function | Description |
|---|---|
| strlen() | Returns the length of the string, not including the null terminator. |
| strcpy() | Copies the string pointed to by the source to the destination, including the null terminator. |
| strncpy() | Copies up to n characters from the source string to the destination. |
| strcat() | Appends the source string to the end of the destination string. |
| strncat() | Appends up to n characters from the source string to the end of the destination string. |
| strcmp() | Compares two strings lexicographically. Returns 0 if equal, a negative value if the first string is less, and a positive value if the first string is greater. |
| strncmp() | Compares up to n characters of two strings lexicographically. |
| strchr() | Searches for the first occurrence of a character in a string. |
| strrchr() | Searches for the last occurrence of a character in a string. |
| strstr() | Searches for the first occurrence of a substring in a string. |
| strtok() | Tokenizes a string into a series of tokens based on delimiters. |
| strspn() | Returns the length of the initial segment of the string that consists only of characters in a specified set. |
| strcspn() | Returns the length of the initial segment of the string that consists of characters not in a specified set. |
| strpbrk() | Searches for the first occurrence of any of the characters in a specified set within a string. |
| strcoll() | Compares two strings according to the current locale. |
| strxfrm() | Transforms a string according to the current locale for use with strcoll(). |
| sprintf() | Formats and stores a series of characters and values in the string. |
| sscanf() | Reads formatted input from a string. |