C String Functions
🔤 C String Functions (Complete Tutorial: Beginner → Advanced)
In C language, strings 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.
1️⃣ What Is a String in C?
A string is an array of characters terminated by a null character
'\0'.
Memory:
2️⃣ Include String Library
3️⃣ 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
4️⃣ String Search Functions 🔍
🔹 strchr() – Find Character
✔ Returns pointer to first occurrence
🔹 strrchr() – Find Last Occurrence
🔹 strstr() – Find Substring ⭐
✔ Output → Programming
5️⃣ String Tokenization 🔥
🔹 strtok() – Split String
✔ Used in CSV parsing
⚠️ Modifies original string
6️⃣ Case Conversion (Non-Standard but Common)
(For strings, loop manually)
7️⃣ String Input & Output Recap
Input
Output
8️⃣ Common String Mistakes ❌
❌ Forgetting '\0'
❌ Using gets() (unsafe)
❌ Buffer overflow in strcpy()
❌ Comparing strings using ==
✔ Correct comparison:
9️⃣ sizeof() vs strlen() ⭐
🔟 Important Interview Questions
-
Difference between
strlen()andsizeof() -
Why
strcmp()is used instead of== -
Is
strcpy()safe? -
How
strtok()works internally -
What 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
