C++ : String Class in hindi , write down classes in string in c++ language with examples and programs

write down classes in string in c++ language with examples and programs , C++ : String Class in hindi :-
इससे  पहले के article मे , string के बसिज़ और input और आउटपुट operation को discuss किया था |अब इस article मे string class और class मे उपस्थित member function को discuss करेगे |
1.Blending of String and Numerical Input
जब किसी numerical input को string के साथ मिक्स किया जाता है तब error generate होती है | उदाहरण के लिए :
#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std;
int house;
cout<< “Enter House Number : “<<endl;
cin>>house;
char street[30];
cout<<“Enter Street Name :”<<endl;
cin.get(street,30);
cout<<“House Number : “<<house<<endl;
cout<<“Street Name : “<< street<<endl;
cout<<“Process Completed ! Forward To Next Form.”<<endl;
getch();
}
इस उदाहरण का आउटपुट होगा जो की error less होगा :
Enter House Number : 1
Enter Street Name : Gandhi Colony
House Number : 1
Street Name :
Process Completed ! Forward To Next Form.
इस उदाहरण मे , street name को read नहीं किया गया क्योकि जब cin का use house number को read करने के लिए गया तब input queue मे new character को insert किया गया है |और cin.get() function से new line character को read करेगा और null value को string street के address पर store करता है|
इस error को दूर करने के लिए कई सारे उपाय है लेकिन सबसे ज्यादा प्रचलित उपाय है |एक get () function को call करना जिसमे कोई भी argument pass नहीं होता है | उदाहरण के लिए
#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std;
int house;
cout<< “Enter House Number : “<<endl;
cin>>house;
cin.get();
char street[30];
cout<<“Enter Street Name :”<<endl;
cin.get(street,30);
cout<<“House Number : “<<house<<endl;
cout<<“Street Name : “<< street<<endl;
cout<<“Process Completed ! Forward To Next Form.”<<endl;
getch();
}
इस उदाहरण का आउटपुट होगा जो की error free होगा :
Enter House Number : 1
Enter Street Name : Gandhi Colony
House Number : 1
Street Name : Gandhi Colony
Process Completed ! Forward To Next Form.
 
String Class
ISO/ANSI C++ मे ,string class को add किया गया जिसमे string से related कई सारे function होते है |String को store करने के लिए character array को use नहीं किया जाता है बल्कि string variable का use किया जाता है |String class का use ,array की तरह सरल होता है |
 
String class को किसी प्रोग्राम मे include करने के लिए String header file को use किया जाता है |string class, std namespace का भाग होती है | इसलिए using namespace std को use किया जाता है |string class की डेफिनिशन छुपी हुई होती है जिससे string class नार्मल variable की तरह use कर सकते है |
 
उदाहरण :
#include<ostream.h>
#include<conio.h>
#include<string>   // header declare //
void main()
{
using namespace std;

 

int house;
cout<< “Enter House Number : “<<endl;
cin>>house;
cin.get();
string street;    // string function use //
cout<<“Enter Street Name :”<<endl;
cin.get(street,30);
string city =”Jaipur”;
cout<< “Enter Pin Code  : “<<endl;
cin>>pin;
cout<<“House Number : “<<house<<endl;
cout<<“Street Name : “<< street<<endl;
cout<<“City Name : “<< city <<endl;
cout<<“Pin Code : “<< pin <<endl;
cout<<“Process Completed ! Forward To Next Form.”<<endl;
getch();
}
 
इस उदाहरण मे आपने काफी सारी string class के concept को observe किया होगा :
1.String class के class object का  initial , C Style String initial की तरह होता है |
         string city =”Jaipur”;
2.cin statement का use string object को read करने के लिए किया जाता है |
          cin.get(street,30);
3.cout का भी use हो सकता है |
          cout<<“City Name : “<< city ;
4.array notation का use string की किसी element को access करने के किया जा सकता है |
          cout<<“First letter of City  : “<<city[0]<<endl;
 
Assignment , Concatenation And Appending
string class मे string से releted कई सारे function होता है |उसमे मे से तीन मुख्य होते है:
1.Assignment
String मे Assignment operation, नार्मल variable assignment की तरह होता है | अगर दो string मे assignment operation perform करना होता है तब assignment operation possible है |लेकिन जब दो character array मे assignment operation perform करना होना तब assignment operation possible नहीं है |उदाहरण के लिए
 #include<ostream.h>
#include<conio.h>
#include<string>   // header declare //
void main()
{
using namespace std;
string name1, name2;
string aad1,add2;
cout<<“Enter Name : “<<endl;
cin>>name1;
cout<<“Enter Name : ” <<end;
cin>>name2;
cin<<Enter Address for ” <<name1<< “:”<<endl;
cin.get(add1);
add2=add1;
cout<<“Address For “<<name2<<“:”<< add2 << endl;
getch();
}
 
इस उदाहरण मे string add1 की value को add2 मे assign की गयी है इसका आउटपुट होगा :
Enter Name : Parth
Enter Name : Om
Enter Address for Parth : Jaipur
Address For Om : Jaipur
अगर दो character array मे डायरेक्ट assignment possible नहीं है |अगर string को array की form मे declare किया जाता है तब assignment operation नहीं हो सकता है |
 
2.Concatenation 
दो string को जोड़ा जा सकता है |इस operation के लिए addition operator ‘+’ का use किया जाता है | shorthand operator ‘+=’ का भी use हो सकता है | इसका उदाहरण मे ,
#include<ostream.h>
#include<conio.h>
#include<string>   // header declare //
void main()
{
using namespace std;
string street;    // string function use //
cout<<“Enter Street Name :”<<endl;
cin.get(street);
string city ;
cout<<“Enter City Name :”<<endl;
cin.get(city);
string address = street + city ; // Concatenation  operation //
cout<<“Address : “<< address << endl;
cout<<“Process Completed ! Forward To Next Form.”<<endl;
getch();
}
इस उदाहरण मे street और city दो string है जिसको string address मे add करके assign किया जाता है जिससे केवल एक cout statement,  address के सभी element ( city,street ) आदि को display करने की लिए उपयुक्त है |
3.Appending
Concatenation  operation से ही appending operation perform हो सकता है |इसके लिए old string को जिस position पर append करना है उससे +1 position पर terminate करदेना चाहिए |और बाद मे Concatenation  operation से append string value और old string को add कर देनी चाहिए | इस operation निन्म उदाहरण से समजा जा सकता है |
#include<ostream.h>
#include<conio.h>
#include<string>   // header declare //
void main()
{
using namespace std;

string first_name;
string surname;
string new_surname;
cout<<“Enter Name :”<<endl;
cin.get(first_name).get();
cout<<“Enter Surname : “<<endl;
cin.get(surname).get();
cout<<“Your Full Name :”<<first_name+surname<<endl;
cout<<“Enter New Surname : ” << endl;
cin.get(new_surname).get();
cout<<“Now Your Full Name :”<<first_name+new_surname<<endl;
getch();

}
इस उदाहरण मे , new_surname से surname को append किया है |इसका आउटपुट होगा :
Enter Name : Ravi
Enter Surname : Sharma
Your Full Name : Ravi Sharma
Enter New Surname : Gupta
Now Your Full Name : Ravi Gupta