C String Functions

C String Functions (Complete Tutorial: Beginner → Advanced)
In C String Functions are handled using character arrays and the string library
Header file: <string.h>
C does not have a built-in string data type, so string functions are very important for real programs and interviews.
What Is a String in C?
A string is an array of characters terminated by a null character
'\0'.
Memory:
Include String Library
Most Common String Functions
strlen() – String Length
- Does NOT count
'\0'
strcpy() – Copy String
- Destination must have enough space
strncpy() – Safer Copy
- Limits copy size
strcat() – Concatenate Strings
- Result →
"Hello World"
strncat() – Safe Concatenation
strcmp() – Compare Strings
- Case-sensitive
strncmp() – Compare Limited Characters
String Search Functions
strchr() – Find Character
- Returns pointer to first occurrence
strrchr() – Find Last Occurrence
strstr() – Find Substring
- Output →
Programming
String Tokenization
strtok() – Split String
- Used in CSV parsing
- Modifies original string
Case Conversion (Non-Standard but Common)
(For strings, loop manually)
String Input & Output Recap
Input
Output
Common String Mistakes
- Forgetting
'\0' Usinggets()(unsafe)- Buffer overflow in
strcpy() Comparing strings using==
Correct comparison:
sizeof() vs strlen()
Important Interview Questions
Difference between
strlen()andsizeof()Why
strcmp()is used instead of==Is
strcpy()safe?How
strtok()works internallyWhat is null-terminated string?
Real-Life Use Cases
Input validation
File parsing
URL & protocol handling
Embedded firmware
Competitive programming
Summary
- Strings are character arrays
<string.h>provides powerful utilities- Always handle buffer size carefully
strcmp()for comparison, not==Extremely important for interviews & projects
