String Functions in C
Basic and Advanced Functions with
Examples
Introduction to Strings in C
• • Strings are arrays of characters ending with
'0'
• • Declared as: char str[20] = "Hello";
• • String functions are defined in <string.h>
strlen()
• Prototype: size_t strlen(const char *str);
• Returns length of string (not counting '0')
• Example: strlen("Hello") → 5
strcpy()
• Prototype: char* strcpy(char *dest, const char
*src);
• Copies src into dest
• Example: dest=""; strcpy(dest,"Hi"); →
dest="Hi"
strncpy()
• Prototype: char* strncpy(char *dest,const
char *src,size_t n);
• Copies first n chars of src to dest
strcat()
• Prototype: char* strcat(char *dest,const char
*src);
• Appends src to dest
strncat()
• Prototype: char* strncat(char *dest,const char
*src,size_t n);
• Appends first n chars of src to dest
strcmp()
• Prototype: int strcmp(const char *s1,const
char *s2);
• Returns 0 if equal, <0 if s1<s2, >0 if s1>s2
strncmp()
• Prototype: int strncmp(const char *s1,const
char *s2,size_t n);
• Compares first n chars
strchr()
• Prototype: char* strchr(const char *str,int c);
• Finds first occurrence of character
strrchr()
• Prototype: char* strrchr(const char *str,int c);
• Finds last occurrence of character
strstr()
• Prototype: char* strstr(const char
*haystack,const char *needle);
• Finds substring in string
strtok()
• Prototype: char* strtok(char *str,const char
*delim);
• Splits string into tokens
strdup()
• Prototype: char* strdup(const char *str);
• Returns a new copy of string (allocated)
strerror()
• Prototype: char* strerror(int errnum);
• Returns error message string
memset()
• Prototype: void* memset(void *str,int c,size_t
n);
• Fills memory with a constant byte
memcpy()
• Prototype: void* memcpy(void *dest,const
void *src,size_t n);
• Copies n bytes from src to dest
memmove()
• Prototype: void* memmove(void *dest,const
void *src,size_t n);
• Safe memory copy, handles overlap
memcmp()
• Prototype: int memcmp(const void *s1,const
void *s2,size_t n);
• Compares first n bytes
strspn()
• Prototype: size_t strspn(const char *s,const
char *accept);
• Returns length of prefix containing only accept
chars
strcspn()
• Prototype: size_t strcspn(const char *s,const
char *reject);
• Returns length of prefix not containing reject
chars
strpbrk()
• Prototype: char* strpbrk(const char *s,const
char *accept);
• Finds first char in s matching any in accept
Example Program
• #include <stdio.h>
• #include <string.h>
• int main() {
• char s1[20] = "Hello";
• char s2[20] = "World";
• strcat(s1, s2);
• printf("%s", s1); // HelloWorld
• return 0;
• }
Best Practices
• • Always allocate enough space for strings
• • Remember the null terminator '0'
• • Prefer fgets() over gets() for input
• • Use strncpy() to avoid buffer overflows
Summary
• • <string.h> provides many useful string
functions
• • Basic: strlen, strcpy, strcat, strcmp, strchr,
strstr
• • Advanced: strtok, strdup, memset, memcpy,
memmove, memcmp, etc.
• • Always handle memory carefully in C

C Programming String library Functions.pdf