C++ Access Strings
๐ C++ Access Strings (Characters Access Karna)
C++ mein string ke individual characters ko access (read / modify) karna bahut common haiโloops, validation, parsing, etc. ke liye.
๐น 1. Indexing Using [] Operator
๐ Index 0 se start hota hai
| Index | Character |
|---|---|
| 0 | S |
| 1 | a |
| 2 | n |
| 3 | j |
| 4 | i |
| 5 | t |
๐น 2. Access Last Character
Output:
๐น 3. Using at() Function (Safe Method)
โ Bounds check karta hai
โ Out of range par exception throw karta hai
๐น 4. Modify String Characters
Output:
๐น 5. Loop Through String (Using Index)
Output:
๐น 6. Range-Based For Loop (Modern C++)
๐น 7. Read-Only Access with const
โ Common Mistakes
โ Safe option:
๐ Summary
-
[]โ Fast, no bounds check -
at()โ Safe, bounds check -
Indexing starts from
0 -
Characters can be read & modified
-
Looping strings is very common
