Index
In C, strings are arrays of characters, ending with a null character '\0'. Strings are handled as arrays and manipulated with a set of built-in functions available in the <string.h> library. Some of the most commonly used string functions include strlen, strcpy, strcat, strcmp, and more.
1. strlen (String Length)
strlen calculates the length of a string, excluding the null terminator.
Syntax:
size_t strlen(const char *str);Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of the string: %zu\n", strlen(str)); // Output: 13
return 0;
}
2. strcpy (String Copy)
strcpy copies one string into another. Be careful with the destination buffer’s size to avoid buffer overflow.
Syntax:
char *strcpy(char *dest, const char *src);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
strcpy(dest, src); // Copies src to dest
printf("Destination string after strcpy: %s\n", dest); // Output: Hello
return 0;
}
3. strcat (String Concatenation)
strcat appends the source string to the destination string. Ensure the destination has enough space.
Syntax:
char *strcat(char *dest, const char *src);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = " World";
strcat(str1, str2); // Appends str2 to str1
printf("Concatenated string: %s\n", str1); // Output: Hello World
return 0;
}
4. strcmp (String Comparison)
strcmp compares two strings lexicographically:
- Returns
0if both strings are equal. - Returns a negative value if the first string is less than the second.
- Returns a positive value if the first string is greater than the second.
Syntax:
int strcmp(const char *str1, const char *str2);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "World";
printf("strcmp(str1, str2) = %d\n", strcmp(str1, str2)); // Output: 0
printf("strcmp(str1, str3) = %d\n", strcmp(str1, str3)); // Output: < 0 (str1 < str3)
return 0;
}
5. strncpy (String Copy with Length)
strncpy copies a specific number of characters from the source string to the destination.
Syntax:
char *strncpy(char *dest, const char *src, size_t n);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
strncpy(dest, src, 3); // Copies first 3 characters of src to dest
dest[3] = '\0'; // Null-terminate manually
printf("Destination after strncpy: %s\n", dest); // Output: Hel
return 0;
}
6. strncat (String Concatenation with Length)
strncat appends up to a specific number of characters from the source to the destination.
Syntax:
char *strncat(char *dest, const char *src, size_t n);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
strncat(str1, str2, 3); // Appends first 3 characters of str2 to str1
printf("Concatenated string: %s\n", str1); // Output: HelloWor
return 0;
}
7. strstr (Find Substring)
strstr finds the first occurrence of a substring in a string. It returns a pointer to the first occurrence, or NULL if not found.
Syntax:
char *strstr(const char *haystack, const char *needle);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *result = strstr(str, "World");
if (result != NULL) {
printf("Substring found: %s\n", result); // Output: World!
} else {
printf("Substring not found.\n");
}
return 0;
}
8. strchr (Find Character)
strchr finds the first occurrence of a character in a string. It returns a pointer to the character, or NULL if not found.
Syntax:
char *strchr(const char *str, int c);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *result = strchr(str, 'W');
if (result != NULL) {
printf("Character found: %s\n", result); // Output: World!
} else {
printf("Character not found.\n");
}
return 0;
}
9. strrchr (Find Last Occurrence of Character)
strrchr finds the last occurrence of a character in a string.
Syntax:
char *strrchr(const char *str, int c);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *result = strrchr(str, 'o');
if (result != NULL) {
printf("Last occurrence of 'o' found at: %s\n", result); // Output: orld!
} else {
printf("Character not found.\n");
}
return 0;
}
10. strdup (Duplicate String)
strdup duplicates a string by allocating memory and copying the contents. It returns a pointer to the new string, which must be freed manually.
Syntax:
char *strdup(const char *str);
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[] = "Hello, World!";
char *dupStr = strdup(str);
if (dupStr != NULL) {
printf("Duplicated string: %s\n", dupStr); // Output: Hello, World!
free(dupStr); // Free allocated memory
} else {
printf("Memory allocation failed.\n");
}
return 0;
}
Summary of String Functions
| Function | Purpose |
|---|---|
strlen | Finds the length of a string |
strcpy | Copies one string to another |
strncpy | Copies a specified number of characters |
strcat | Appends one string to another |
strncat | Appends a specified number of characters |
strcmp | Compares two strings |
strstr | Finds a substring in a string |
strchr | Finds the first occurrence of a character |
strrchr | Finds the last occurrence of a character |
strdup | Duplicates a string |

