C++ C-Style Strings
🧵 C++ C-Style Strings
C-Style Strings wo strings hoti hain jo character array (char[]) ke form mein hoti hain aur null character '\0' se end hoti hain.
Ye C language se aayi hui strings hain, isliye inhe C-style strings kehte hain.
1. What is a C-Style String?
Memory mein ye aise store hota hai:
➡ '\0' batata hai ki string yahin end ho gayi.
2. Declare & Initialize
Method 1: String literal
Method 2: Character array
3. Print C-Style String
4. User Input (C-Style String)
⚠️ Space ke baad input stop ho jaata hai.
5. Input with Spaces (cin.getline)
✔ Space ke saath poori line read karta hai.
6. Common C-String Functions
(Need header: #include <cstring>)
| Function | Work |
|---|---|
strlen() | Length nikalta hai |
strcpy() | Copy string |
strcat() | Join strings |
strcmp() | Compare strings |
Example: strlen()
Example: strcpy()
Example: strcat()
Example: strcmp()
7. Modify C-Style String
❌ Common Mistakes
✔ Correct:
8. C-Style String vs std::string
| C-Style String | std::string |
|---|---|
char[] based | Class based |
| Manual size handling | Automatic resizing |
| Error-prone | Safe & easy |
| Faster (low-level) | Preferred in modern C++ |
📌 Summary
C-style strings =
chararrays +'\0'cstringfunctions se operations hote hainSize carefully manage karna padta hai
Modern C++ mein
std::stringpreferred
