### String Library in C
C provides a library of functions for working with strings in the `string.h` header file. These functions
make it easier to manipulate strings, which are essentially arrays of characters terminated by a null
character (`'\0'`).
#### Commonly Used String Library Functions
| Function | Description |
|--------------------|-------------|
| `strlen()` | Returns the length of a string (excluding the null character). |
| `strcpy()` | Copies one string to another. |
| `strncpy()` | Copies the first `n` characters of one string to another. |
| `strcat()` | Appends one string to another. |
| `strncat()` | Appends the first `n` characters of one string to another. |
| `strcmp()` | Compares two strings lexicographically. |
| `strncmp()` | Compares the first `n` characters of two strings. |
| `strchr()` | Finds the first occurrence of a character in a string. |
| `strrchr()` | Finds the last occurrence of a character in a string. |
| `strstr()` | Finds the first occurrence of a substring in a string. |
| `strtok()` | Splits a string into tokens based on delimiters. |
| `memset()` | Fills a block of memory with a particular value (often used with strings). |
| `strrev()` (Non-standard) | Reverses a string (available in some compilers like Turbo C). |
---
#### Example of `strlen()` - String Length
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of the string: %zu\n", strlen(str)); // %zu for size_t
return 0;
```
**Output:** Length of the string: 13
---
#### Example of `strcpy()` - String Copy
```c
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[50];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
```
**Output:** Copied string: Hello, World!
---
#### Example of `strcat()` - String Concatenation
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[] = ", World!";
strcat(str1, str2); // Appends str2 to str1
printf("Concatenated string: %s\n", str1);
return 0;
```
**Output:** Concatenated string: Hello, World!
---
#### Example of `strcmp()` - String Comparison
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result < 0) {
printf("String 1 is less than String 2.\n");
} else {
printf("String 1 is greater than String 2.\n");
return 0;
```
**Output:** String 1 is less than String 2.
---
#### Example of `strchr()` - Find a Character in a String
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr = strchr(str, 'o');
if (ptr) {
printf("First occurrence of 'o' is at position: %ld\n", ptr - str);
} else {
printf("'o' not found in the string.\n");
return 0;
```
**Output:** First occurrence of 'o' is at position: 4
---
#### Example of `strtok()` - Tokenize a String
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello,World,This,Is,C";
char *token = strtok(str, ",");
printf("Tokens:\n");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
return 0;
```
**Output:**
Tokens:
Hello
World
This
Is
---
#### Example of `strstr()` - Find a Substring
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr = strstr(str, "World");
if (ptr) {
printf("Substring found: %s\n", ptr);
} else {
printf("Substring not found.\n");
return 0;
```
**Output:** Substring found: World!
---
#### Example of `strrev()` - Reverse a String
```c
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int n = strlen(str);
for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
int main() {
char str[] = "Hello, World!";
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
```
**Output:** Reversed string: !dlroW ,olleH
---
#### Use Cases of String Library
1. Manipulating strings in text processing.
2. Parsing input data.
3. Building and analyzing tokens.
4. Searching and sorting strings in arrays.