Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟡 Control Flow  ·  Lesson 22

Strings in C++

C++ में strings

String text है — characters का एक क्रम। आधुनिक C++ आपको std::string type देता है (<string> से), जो आपके लिए text और उसकी memory प्रबंधित करता है। यह पुरानी C-शैली के character arrays से कहीं आसान है।

Strings बनाना

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Amit";
    string empty;              // "" se shuru hota hai
    cout << name << "\n";
    return 0;
}
Output:
Amit

String input पढ़ना

Spaces वाली पूरी line पढ़ने को getline इस्तेमाल करें; cin >> केवल एक शब्द पढ़ता है:

C++
string full;
cout << "Enter full name: ";
getline(cin, full);
cout << "Hello, " << full << "\n";
Run: Enter full name: Amit Kumar
Output:
Hello, Amit Kumar

Strings जोड़ना

Strings जोड़ने को + या += इस्तेमाल करें — कोई विशेष functions नहीं चाहिए:

C++
string first = "Amit", last = "Kumar";
string full = first + " " + last;
full += "!";
cout << full;
Output:
Amit Kumar!

Length और characters

C++
string s = "Hello";
cout << s.length() << "\n";   // 5
cout << s[0] << "\n";         // H
s[0] = 'J';                   // ek character badlein
cout << s;                     // Jello
Output:
5
H
Jello

उपयोगी methods

Methodयह क्या करता है
s.length() / s.size()Characters की संख्या
s.substr(pos, len)String का हिस्सा निकालें
s.find("x")Substring की स्थिति (या npos)
s.empty()True अगर string में कोई character न हो
s.append("x")अंत में जोड़ें

C-style strings पर एक नोट

C++ पुरानी C-style strings भी समर्थन करता है — char के arrays जो '\0' null character पर खत्म होते हैं, <cstring> के functions के साथ इस्तेमाल। आप इन्हें पुराने code में देख सकते हैं, पर नए programs के लिए std::string सुरक्षित और पसंदीदा है।

आम गलतियाँ

  • std::string इस्तेमाल करते समय #include <string> भूलना।
  • पूरे नाम के लिए cin >> s इस्तेमाल करना और केवल एक शब्द पकड़ना।
  • std::string और C-style functions को गलत तरीके से मिलाना।
  • s[i] से range के बाहर जाना; जाँची गई access के लिए .at(i) इस्तेमाल करें।
🏋️ अभ्यास

एक पूरा नाम getline से पढ़ें, फिर उसकी length, उसका पहला character, और नाम "Last, First" रूप में substr तथा find इस्तेमाल करके print करें।

सारांश

  • Text के लिए <string> से std::string इस्तेमाल करें।
  • getline(cin, s) पूरी line पढ़ता है; cin >> s एक शब्द।
  • Strings को + और += से जोड़ें।
  • .length() size देता है; s[i] characters access करता है।
  • नए code में C-style char arrays के बजाय std::string पसंद करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में string क्या है?
आधुनिक C++ में string <string> header के std::string type का एक object है, जो आपके लिए text संग्रहीत और प्रबंधित करता है। यह अपने आप बढ़-घट सकता है, पुरानी C-शैली के character arrays के विपरीत, text संभालना कहीं आसान और सुरक्षित बनाते हुए।
C++ में spaces वाली string कैसे पढ़ें?
getline(cin, str) इस्तेमाल करें, जो Enter दबाने तक spaces सहित पूरी line पढ़ता है। Extraction operator cin >> str पहले space पर रुक जाता है, तो यह केवल एक शब्द पकड़ता है।
C++ में strings को concatenate कैसे करें?
std::string के साथ आप बस + operator या += इस्तेमाल करते हैं, जैसे string full = first + " " + last;। यह टुकड़ों को एक नई string में जोड़ता है, जो C-शैली strings के लिए ज़रूरी functions से कहीं सरल है।
C++ में string की length कैसे पता करें?
std::string पर .length() या समकक्ष .size() call करें, जैसे name.length()। दोनों characters की संख्या लौटाते हैं, और आप name[i] या name.at(i) से अलग-अलग characters access कर सकते हैं।
C++ में std::string और C-style string में क्या अंतर है?
std::string एक प्रबंधित object है जो अपनी memory और size खुद संभालता है, जबकि C-style string char का एक सादा array है जो null character पर खत्म होता है। std::string सुरक्षित और आसान है, तो यह पसंदीदा है जब तक आप पुराने C code से interface न कर रहे हों।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicFunctions in C++ आपकी प्रगति: 22 / 81 (27%) · Lesson 23 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें