C string.h Library

C string.h Library
In C string.h Library provides functions to manipulate and handle strings (character arrays). It’s one of the most widely used libraries for string operations.
📌 Common String Functions
1️⃣ String Length and Copy
| Function | Description |
|---|---|
strlen(const char *str) | Returns the length of the string (excluding \0) |
strcpy(char *dest, const char *src) | Copies src string into dest |
strncpy(char *dest, const char *src, size_t n) | Copies first n characters of src to dest |
Example:
2️⃣ String Concatenation
| Function | Description |
|---|---|
strcat(char *dest, const char *src) | Appends src at the end of dest |
strncat(char *dest, const char *src, size_t n) | Appends first n characters of src to dest |
Example:
3️⃣ String Comparison
| Function | Description |
|---|---|
strcmp(const char *str1, const char *str2) | Compares two strings, returns 0 if equal |
strncmp(const char *str1, const char *str2, size_t n) | Compares first n characters |
strcasecmp / strncasecmp | Case-insensitive comparison (POSIX) |
Example:
4️⃣ Searching in Strings
| Function | Description |
|---|---|
strchr(const char *str, int c) | Returns pointer to first occurrence of c |
strrchr(const char *str, int c) | Returns pointer to last occurrence of c |
strstr(const char *haystack, const char *needle) | Returns pointer to first occurrence of needle substring |
Example:
5️⃣ String Tokenization
| Function | Description |
|---|---|
strtok(char *str, const char *delim) | Splits string into tokens based on delimiters |
Example:
6️⃣ String Transformation
| Function | Description |
|---|---|
toupper(int c) / tolower(int c) | Convert character case |
memset(void *ptr, int value, size_t num) | Fill memory with value |
memcpy(void *dest, const void *src, size_t n) | Copy memory block |
memcmp(const void *ptr1, const void *ptr2, size_t n) | Compare memory block |
Example:
7️⃣ Dynamic String Example
📌 Summary Table
| Function | Purpose |
|---|---|
strlen | Get string length |
strcpy, strncpy | Copy strings |
strcat, strncat | Concatenate strings |
strcmp, strncmp | Compare strings |
strchr, strrchr | Find character in string |
strstr | Find substring |
strtok | Tokenize string |
toupper, tolower | Convert character case |
memset, memcpy, memcmp | Manipulate memory |
✅ Notes
Strings in C are arrays of characters ending with
\0.Always ensure enough space in destination arrays for
strcpy/strcat.Prefer
strncpyandstrncatfor safe operations.Use dynamic memory for strings of unknown length.
