What is String library functions in C:
- In C, the string library provides a set of functions that allow you to work with strings.
- These functions are declared in the header file.
Some commonly used string library functions in C:
1. strlen():
- Calculates the length of a string (excluding the null terminator).
C
#include <string.h>
size_t strlen(const char* str);
2. strcpy():
- Copies a string from one location to another.
C
#include <string.h>
char* strcpy(char* destination, const char* source);
3. strcat():
- Concatenates (appends) one string to the end of another.
C
#include <string.h>
char* strcat(char* destination, const char* source);
4. strcmp():
- Compares two strings and returns an integer indicating their relationship (lexicographically).
C
#include <string.h>
int strcmp(const char* str1, const char* str2);
5. strncmp():
Compares two strings up to a specified number of characters and returns an integer indicating their relationship.
C
#include <string.h>
int strncmp(const char* str1, const char* str2, size_t n);
6. strchr():
- Searches for the first occurrence of a character in a string and returns a pointer to that location.
C
#include <string.h>
char* strchr(const char* str, int character);
7. strstr():
- Searches for the first occurrence of a substring in a string and returns a pointer to that location.
C
#include <string.h>
char* strstr(const char* str1, const char* str2);
8. sprintf():
- Formats and stores a series of characters into a string (similar to printf).
C
#include <stdio.h>
int sprintf(char* str, const char* format, ...);