String in C ++

C++ में string एक object है जो sequence of character का represent करती है। दूसरी ओर, C programming में string को character की एक array द्वारा रिप्रेजेंट किया जाता है। C++ C-style string का भी support करता है। C++ में string object को character array की तुलना में more frequently उपयोग किए जाते हैं क्योंकि string object built-in function की wide range का समर्थन करते हैं।

Syntax:

C++ में string बनाने के लिए syntax काफी simple है। हम C++ में string बनाने के लिए string keyword का उपयोग करते हैं। हालाँकि, हमें string keyword का उपयोग करने से पहले अपने program में standard string class को भी include करना होता है।

string str_name = "HERE WE DECLARE C++ STRING";

The C style character string :

C programming language में, string को character array के रूप में defined किया जाता है जो एक null\termination character (\0) के साथ end होता है।termination important है क्योंकि यह compiler को बताती है कि string कहाँ end होती है।C ++ , C-style string  का भी support करता है।

Syntax :

हम C-style के string वैसे ही बना सकते हैं जैसे हम एक integer array create करते हैं।

char str_array[7] = {'s','c','a','l','e','r','\0'};

क्योंकि हमें array में एक null character भी add है, array की length string की length से एक अधिक है। Alternatively , हम उपरोक्त string को इस तरह भी define कर सकते हैं:

char str_array[] = "Scaler";

The following code is to take string input :

#include<iostream>
#include<string>
using namespace std;
int main()
{ 
 string s;
 cout<<"Enter a string: ";
 cin>>s;
 cout<<"\ns holds: "<<s;
 return 0;
 }

string class string function की एकrich library के साथ आती है जो string manipulation और अन्य processes में काम आ सकती है। हम C++ supported कुछ functions देखेंगे

1. string ::length():

string की length return करता है।

string s="DiplomaWarriors";
cout<<s.length(); // prints 15

2. string :: find():

character के first index को return करता है

string s="DiplomaWarriors";
cout<<s.find('D'); // prints 15

3. string ::substr():

index को as a parameter accept करता है और उस index से last तक substring return करता है।

string s="DiplomaWarriors";
cout<<s.substr(7); // prints Warriors

4. string :: at():

index को parameter के रूप में accept करता है और उस index परavailable character return करता है।

string s="DiplomaWarriors";
 cout<<s.at(7); // prints W

5. string ::append():

Parameter के रूप में copy करने के लिए एक और string object, index, length, number of character को accept करता है और defined parameter को string के end तक push करता है।

string s;
string str="DiplomaWarriors";
s.append(str);
cout<<s; // prints DiplomaWarriors

6. string ::getline():

istream object और string object को parameter के रूप में accept करता है और istream object से character extract करता है और उन्हें एक string object में store करता है जब तक कि एक delimiter character नहीं मिल जाता।

string s;
cout<<"Enter the string: ";
getline(cin,s);
cout<<"\ns holds: "<<s;

7. string :: compare():

string object को एक parameter के रूप में accept करता है और 0 या अन्य integer return करता है ,यदि string respectively match नहीं करता है।

string s="takeuforward";
string str="striver";
s.compare(str) ? cout<<"doesn't match" : cout<<"matches";

8. string :: clear():

String को clear करता है और इसे एक empty string बनाता है

string s="DiplomaWarriors";
s.clear();
cout<<s; // prints “”

9. string :: front():

string के first character को access करने के लिए उपयोग किया जा सकता है।

string s="DiplomaWarriors";
cout<<s.front(); // prints D

10. string ::back():

string के last character को access करने के लिए उपयोग किया जा सकता है।

string s=”Diplomawarriors”; cout<<s.back(); // prints s

String class vs character array :

String Character array

एकclass उन objects को define करता है
जिन्हें character के sequence द्वारा represent जा सकता है।

यह ‘\0’ के साथ terminate होने वाले character का एक sequence है

memory dynamically allocate की जाती है।

memory statically allocate की जाती है।

string manipulation के लिए functions का एक rich library प्रदान करता है।

string manipulation के लिए many inbuilt functions नहीं हैं।

Implementation slower है।

Implementation faster है।

 

Previous articleStorage Class in C++

LEAVE A REPLY

Please enter your comment!
Please enter your name here