Strings
String एक sequence of characters (अक्षरों का क्रम) होता है। इसमें Alphabets (A–Z, a–z), Digits (0–9), Special Symbols (@, #, $, ! आदि) और Spaces भी शामिल हो सकते हैं।
Programming में जब भी हम किसी भी text को double quotes (” “) के अंदर लिखते हैं, तो वह अपने-आप String बन जाता है।
Table of Contents
ToggleExample :
“Hello World” → यह एक String है।
“12345” → यह भी String है (क्योंकि यह double quotes के अंदर लिखा है)।
“my_name@example.com” → यह भी एक valid String है।
Types of Strings in C++ (प्रकार)
1. C-Style Strings (Character Array Strings)
C-Style String एक character array होता है, जिसके अंत में ‘\0’ (null terminator) लगाया जाता है। यही null character इस बात का संकेत देता है कि string कहाँ खत्म हो रही है।
Key Points:
- यह method C language से लिया गया है।
- String को character array में store किया जाता है।
- Null terminator mandatory होता है।
- Size fixed होता है।
- Functions जैसे strlen(), strcpy(), strcmp() आदि का उपयोग होता है।
Example :
char name[10] = “India”;
Memory representation :
2. C++ String Class (std::string)
C++ String Class (std::string) एक high-level string data type है, जो characters को dynamic memory में store करता है और text को manipulate करने के लिए powerful functions provide करता है। इसका size runtime पर automatically बढ़ सकता है और null terminator की handling internally होती है।
Example :
#include <iostream>
#include <string>
using namespace std;
int main() {
string city = “New Delhi”;
cout << city;
return 0;
}
Memory Representation
C++ string class internally dynamic memory (heap) में characters store करती है, और object में pointers + metadata रहता है।
+————————–+
| string city object |
+————————-+
| Pointer → 0xABF310 | —> Heap Memory:
| Length = 9 | +———————-+
| Capacity = 15 | —> | N e w D e l h i \0 |
+—————————+ +———————-+
C++ String Class Essential Operations
C++ String Class (std::string) के essential operations वो basic actions हैं, जो text data को manipulate, modify और analyze करने के लिए perform किए जाते हैं। इन operations से strings को आसानी से create, update, search, compare और extract किया जा सकता है।
1. String Initialization / Declaration : String को create और initialize करना।
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = “CyberDairy”; // Using assignment
string city(“New Delhi”); // Using constructor
string emptyString; // Empty string
cout << name << ” ” << city << endl;
return 0;
}
2. Concatenation (जोड़ना) : दो या अधिक strings को जोड़ना।
string a = “Hello “;
string b = “World”;
// Using + operator
string c = a + b;
// Using append()
a.append(b);
cout << c << endl; // Output: Hello World
cout << a << endl; // Output: Hello World
3. Accessing Characters (Character Access) : String के किसी specific position का character access करना।
string s = “New Delhi”;
char ch1 = s[0]; // Accessing the first character of the string using [] operator and Index 0 represents the first character in the string
char ch2 = s.at(1); // Accessing the second character of the string using at() function and at(1) means the character present at index 1
// Printing both characters
cout << ch1 << ” ” << ch2 << endl; // Output: N e
4. Modifying Characters : String के किसी character को बदलना।
s[0] = ‘M’; // New Delhi → Mew Delhi
cout << s << endl;
5. Length / Size : String में कितने characters हैं, जानना।
int len = s.length();
int sz = s.size();
cout << “Length: ” << len << “, Size: ” << sz << endl;
6. Searching in String : किसी character या substring की position ढूँढना।
int pos = s.find(“Dairy”);
if(pos != string::npos)
cout << “‘Dairy’ found at index: ” << pos << endl;
Most Used C++ String Functions
Function / Method | Description | Example |
length() / size() | String की length return करता है | str.length() |
empty() | Check करता है string empty है या नहीं | str.empty() |
append() | String के end में जोड़ना (concatenate) | str.append(” C++”) |
insert(pos, str) | किसी specific position पर string insert करना | str.insert(3, “X”) |
erase(pos, len) | String में से characters हटाना | str.erase(0,3) |
replace(pos, len, str) | String का कोई part replace करना | str.replace(0,3,”Hi”) |
substr(pos, len) | String का substring निकालना | str.substr(0,4) |
find(str) | Substring की position ढूँढना | str.find(“Hello”) |
c_str() | std::string को C-style string में convert करना | str.c_str() |
stoi() | String को integer में convert करना | stoi(“123”) |
to_string() | Integer/float को string में convert करना | to_string(123) |
compare() | दो strings को lexicographically compare करना | str1.compare(str2) |
at(pos) | Safe तरीके से किसी position का character access करना | str.at(2) |
push_back(char) | Last में character add करना | str.push_back(‘!’) |
pop_back() | Last character remove करना | str.pop_back() |
Advantages (लाभ)
- Easy to use : Built-in functions और operators के साथ आसानी से manipulate किया जा सकता है।
- Dynamic size : Memory automatically adjust होती है, resizing के लिए extra code नहीं चाहिए।
- Safe : Buffer overflow की risk कम होती है compared to C-style strings।
- Flexible operations : Concatenation, comparison, substring, search और replace आसानी से perform किया जा सकता है।
- Readable code : ‘+’ operator और intuitive functions से clean code बनता है।
- STL friendly : STL containers और algorithms के साथ easily integrate होता है।
Disadvantages (सीमाएँ)
- Slightly slower : कुछ cases में char array की तुलना में performance कम हो सकती है।
- Memory overhead : Dynamic allocation और internal bookkeeping के कारण extra memory use होता है।
- Less low-level control : Direct memory manipulation C-style strings जितना possible नहीं।
- Function overhead : Built-in string functions minor processing delay add कर सकते हैं।






