Thursday, December 5, 2019

Strings in C++

Introduction to C++ Strings
Strings characters की sequence होती है। आसान शब्दों में कहें तो जब अक्षरों को एक क्रम में लिखा जाता है तो string बनती है। उदाहरण के लिए Happy Republic Day एक string है।

C++ में किसी अक्षर को store करने के लिए आप char type का variable create करते है। लेकिन strings को store करने के लिए C++ कोई भी built in data type provide नहीं करती है।

हालाँकि आप char array के माध्यम से strings store कर सकते है लेकिन ये एक पुराना तरीका है जो C Language में यूज़ किया जा चूका है। साथ ही जब आप strings को char array के माध्यम से store करते है तो उन strings पर operations perform करना बहुत difficult हो जाता है।

ऐसे में C++ आपको string class provide करती है जिससे आप strings को store भी कर सकते है और उन पर operations भी perform कर सकते है। आइये इसके बारे में जानने का प्रयास करते है।

C++ String Class

जैसा की मैने आपको पहले बताया strings को store करने और उन पर different operations perform करने के लिए C++ आपको string class provide करती है। String class के object को किसी built in type के variable की तरह ही handle किया जा सकता है।
String class में 3 constructors available है। पहला constructor empty string object create करने के लिए है। इसका syntax नीचे दिया जा रहा है।

String();

दूसरा constructor argument के रूप में pass की गयी string से string object create करता है। इसका syntax इस प्रकार है।

String(char *str); 

तीसरा constructor argument के रूप में एक string object ही लेता है और उसकी string से नए object को create करता है। इसका syntax नीचे दिया जा रहा है।

String(string &str); 

किसी भी program में string class को यूज़ करने के लिए आपको file include करनी पड़ती है। आइये अब देखते है की आप किस प्रकार string objects create कर सकते है। 

Creating String Objects 

String class के objects आप किसी normal class के objects की तरह ही create कर सकते है। लेकिन क्योंकि string class में 3 constructors defined है इसलिए आप string objects भी 3 तरह से create कर सकते है।

#include
#include
int main()
{
    // Will call constructor with char argument 
    string st1(“Happy Republic Day”);  
    // Will call normal constructor & create empty string

    string st2;  


    // Will call constructor with string object argument

    st2 = st1; 
    cout<
    cout<
   
    return 0;

ऊपर दिया गया programe का आउटपुट यह होगा -

                          Happy Republic Day

                          Happy Republic Day

Functions Of String Class

String class आपको वो सभी built in functions provide करती है जो आपको strings के साथ work करने के लिए necessary होते है। जब भी आप string class का कोई object create करते है तो ये functions आपको automatically available हो जाते है।


Functions 
Explanation 
begin() 
ये एक iterator type का function है। ये function call किये जाने पर string की beginning का pointer return करता है।    
end()   
ये भी एक iterator type का function है। ये function call होने पर string का end pointer return करता है।    
append()  
यदि आप किसी एक string के आखिर में कोई दूसरी string जोड़ना चाहते है तो append() function यूज़ कर सकते है। इस function को string object के साथ call किया जाता है और argument के रूप में आप कोई दूसरी string या string object pass कर सकते है। Ex. strngobj1.append(strngobj2);       
at()  
यदि आप string में particular location से किसी character को retrieve करना चाहते है तो इसके लिए at() function यूज़ कर सकते है। उदाहरण के लिए यदि आप HELLO string में से तीसरे number का character retrieve करना चाहते है तो आप इस प्रकार at() method को call करेंगे। strngobj.at(3)        
compare()  
यदि आप किन्हीं दो strings को equality के लिए compare करना चाहते है तो compare() method यूज़ कर सकते है। इस method को आप string object से call करते है और argument के रूप में दूसरी string pass करते है। Ex. strngobj1.compare(strngobj2);     
empty() 
यदि आप पता करना चाहते है की कोई string empty() है या नहीं तो आप empty() function call कर सकते है। यदि string empty होती है तो ये function true return करता है। Ex. strngobj.empty();    
erase()
यदि आप string से कुछ characters erase करना चाहते है तो इसके लिए erase() function यूज़ कर सकते है। इस function में 2 argument pass किये जाते है। पहला argument वह location होती है जँहा से आप characters को remove करना चाहते है। दूसरा argument जितने characters आप remove करना चाहते है उनकी सँख्या होती है। उदाहरण के लिए HELLO string में 3rd location से 2 character remove करने के लिए आप इस प्रकार function call कर सकते है।
Ex. strngobj.erase(3,2);     
find()
यदि आप किसी string में कोई sub-string find करना चाहते है तो इसके लिए आप find() method यूज़ कर सकते है। इस method में आप सीधे sub-string या कोई object pass कर सकते है।
Ex. strngobj.find(“substring”);       
length()
ये function string की length return करता है।  
swap()  
यदि आप किन्हीं दो objects की string को आपस में swap करना चाहते है तो इसके लिए swap() function यूज़ कर सकते है। इस function में आप दूसरा string object argument के रूप में pass करते है और इसकी value call किये जाने वाले string object की value से swap हो जाती है।  Ex. strngobj1.swap(strngobj2)     




No comments:

Post a Comment

how to add watermark in movavi video editor | movavi watermark | how to ...

What is static keywords in java ? static keywords in java is a main topic of java.