string in C++
सरल शब्दों में, एक string , characters का एक sequence या एक sentence है अर्थात् किसी व्यक्ति का नाम, स्थान का नाम या message string के उदाहरण हैं –
उदाहरण के लिए, anand , india , hello world string के उदाहरण हैं।
,
C ++, दो प्रकार के string प्रदान करता है
- C-string or C style string
- C++ string
C string in hindi
C -string एक character array है , एक single dimentional array क्योंकि C ++ C का अपडेटेड वर्जन है इसलिए C ++ भी इन सभी libraries को support करता है। इसके अलावा C ++ की अपनी string library भी है।
string को समझने के लिए class
का concept समझना अनिवार्य है। नीचे दिए गए सभी program C-string के library function के उदाहरण हैं.
initialization of a C-string in C++
char string[ ] = "Rahul";
char string[6] = {'R','a','h','u','l',\0'};
char string[6] = "Rahul";
char *string = "Rahul"; // using pointer
जहां string एक identifier है। यदि null character को initialize नहीं किया गया है, तो compiler खुद से इसे implement कर देता है।
C++ string hindi
modern C ++ में, ANSI / ISO मानक C ++ द्वारा एक नई string class को जोड़ा गया है
जिसे C++ string library कहा जाता है। यह string library class और object के mechanism द्वारा implement होता है। इसके syntax में class data type की जगह लेता है।
आइए यहां इसकी व्याख्या करते हैं,
C ++ string, C-string को बेहतर बनाता है और इसे आसान बनाता है। आप इसे नीचे C-string और C ++ स्ट्रिंग के बीच नीचे अंतर से समझ सकते हैं।
जैसे कि string को C++ library में एक class के रूप में पहले से defined किया गया है, इसलिए हम सबसे पहले इसका object declare करते हैं-
string object; //string constructor with no arguments
जहां string एक pre-defined class है और object एक identifier । यहां हम केवल object के माध्यम से data pass करते हैं। जैसे class में-
string object("string"); // one arguments
यह built-in data-type के behaviour को भी support करता है-
string object = "string";
string operation करते समय, निम्नलिखित syntax का उपयोग किया जाता है –
object.string-function(arguments);
जहां string -function एक string class का एक public member है। इसके अलावा, string library में कई constructor , string class के member , operator , iterators होते हैं। इसलिए modern string को समझने के लिए class और object को समझना जरुरी है।
✍ C -string में हम सीधे string के pre -defined function को बिना किसी प्रतिबंध access करते हैं जबकि C ++ string में सभी task, class और object के माध्यम से perform किये जाते हैं।
Turbo C ++ (Borland compiler , C ++ 98) इस modern string को support नहीं करता है, और जैसा कि पहले ही उल्लेख किया गया है C ++, C string को support करता है, इसलिए यहाँ C-string के उदाहरण दिए गए हैं-
ऊपर दिए गए syntax से confuse ना हों, C string में एक string एक identifier है जबकि C ++ string में, एक string एक class होगा।
यहाँ पर आप modern string library के उदाहरण को देख सकते हैं।
How to read a string in C++?
जैसा कि हम जानते हैं, user input को read करने के लिए cin-statement का उपयोग किया जाता है, लेकिन यहाँ हम cin-statement का उपयोग नहीं कर सकते हैं। क्यों?
जब आप string को store करने के लिए cin
-statement का use करते हैं, तो यह असमर्थ होता है, क्योंकि cin
space read नहीं कर सकता है। C ++ में string read करने के लिए हम get()
और getline()
function का उपयोग करते हैं,
उदाहरण के लिए,
यदि हम एक cin
statement से एक string “anand bisht ” store करना चाहते हैं, तो केवल “anand ” ही store होगा , जबकि get()
और getline()
दोनों complete नाम store करेंगे।
इन statements का syntax niche दिया गया है –
gets(variable);
cin.getline(variable, size);
यहां एक उदाहरण है जहां हम C ++ में एक complete string को store करते हैं। दोनों function header file stdio
declare होते हैं इसलिए program में, हमें इनकी header file को include करना होता है
benefit of string
- Program में कोई statement नहीं लिखना पड़ता है जिससे program की readability बनी रहती है।
- क्योंकि program की readability बनी रहती है, इसलिए program को debug करना आसान हो जाता है।
- क्योंकि extra line of code कम हो जाता है, तो program प्रोग्राम का size भी घट जाता है।
- एक program में, हमstring के string library function का उपयोग करते हैं, जिससे समय की बचत होती है।
✍ string library के बिना भी हम string related task कर सकते हैं, लेकिन यह जटिल हो जाता है। इसका उदाहरण character array में दिया गया। है
string library में बहुत सारे string function pre – defined हैं। इनमें से कुछ महत्वपूर्ण function के नाम और उनके कार्य नीचे दी गई table में दिए गए हैं-
C string library Function
Function | Operation |
strlen(str) | Returns the length of str, excluding ‘\0’ |
strcmp(str1, str2) | Compare two strings |
stricmp(str1, str2) | Compare two string without case sensitivity |
strncmp(str1, str2, n) | Compare n characters of each string |
strnicmp(str1, str2, n) | Compare n characters of each string without case sensitivity |
strcpy(str1, str2) | Copies string str2 to str1 |
strncpy(str1, str2, n) | Copies n characters from string str2 into str1. |
strcat(str1, str2) | Append str2 into str1 |
strncat(str1, str2, n) | Append n characters of one string str2 into second-string str1 |
strcspn(str1, str2) | Returns the number of initial consecutive characters of str1 that are not in str2 |
strspn(str1, str2) | Returns the number of initial consecutive characters of str1 that are in str2 |
strlwr(str) | Converts a lowercase string into uppercase |
strupr(str) | Converts a lowercase string into uppercase |
strchr(str1, c) | scan str1 in the forward direction for a Specific character c |
strrchr(str1, c) | scan str1 in the reverse direction for a Specific character c |
strdup(str) | Copies a string into a newly created location |
strrev(str) | Reverse a string in the reverse order except ‘\0’. |
strstr(str1, str2) | scan the sub-string str2 in the str1. |
example follows,
C++ strlen example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str= "rahul singh";
int str_len;
//calculating string length by strlen
str_len = strlen(str);
cout<<"size: "str_len;
getch();
}
OUTPUT
size: 11
C++ strcmp example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[] = "Rahul"; //*str1
char str2[] = "rahul"; //*str2
if(strcmp(str1,str2)==0)
cout<<"string Matched";
else
cout<<"string not matched";
getch();
}
OUTPUT
string not matched
C++ stricmp example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[] = "Rahul"; //*str1
char str2[] = "rahul"; //*str2
if(stricmp(str1,str2)==0) //compare string without case sensitivity
cout<<"Matched..";
else
cout<<"Not Matched..";
getch();
}
OUTPUT
Matched..
C++ strncmp example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[] = "Rahul"; //*str1
char str2[] = "Rahul singh"; //*str2
if(strncmp(str1,str2,4)==0) // compare only first 5 characters
cout<<"Matched..";
else
cout<<"Not Matched..";
getch();
}
OUTPUT
Matched..
C++ strnicmp example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
//different string
char *str1 = "Rahul"; //str1[]
char *str2 = "rahul"; //str2[]
if(strnicmp(str1,str2,4)==0) // compare only first 5 characters
cout<<"Matched..";
else
cout<<"Not Matched..";
getch();
}
OUTPUT
Matched..
C++ strcpy example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char *str1 = "Rahul";
char *str2;
strcpy(str2,str1); //copy str1 to str2
cout<<"str1: "<<str1<<"\n";
cout<<"str2: "<<str2;
getch();
}
OUTPUT
str1: Rahul
str2: Rahul
C++ strncpy example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[] = "Rahul Singh sherma";
char *str2;
strncpy(str2,str1,11); //copy only first 11 characters str1 to str2
cout<<"str1: "<<str1<<"\n";
cout<<"str2: "<<str2;
getch();
}
OUTPUT
str1: Rahul Singh sherma
str2: Rahul Singh
C++ strcat example
#include <string.h>
#include <conio.h>
#include <iostream.h>
void main(void)
{
clrscr();
char *str1 = "Rahul";
char *str2 = " ";
char *str3 = "Singh";
//append all str2 and str3 to str1
strcat(str1, str2);
strcat(str1, str3);
cout<<"str1: "<<str1;
getch();
}
OUTPUT
str1: Rahul Singh
C++ strncat example
In the *str3 sherma will be skip
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str1 = "Rahul";
char *str2 = " ";
char *str3 = "Singh sherma";
//append 1 character from str2 into str1
strncat(str1,str2,1);
//append only first 5 characters from str3 into str1 also
strncat(str1,str3,5);
//
cout<<str1;
getch();
}
OUTPUT
str1: Rahul Singh
C++ strspn example
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main(void)
{
clrscr();
char *str1 = "rahul singh";
char *str2 = "rahul sherma";
int len;
len = strspn(str1, str2);
cout<<"Character where strings different is at position: "<<len;
getch();
}
OUTPUT
Character where strings different is at position: 7
C++ strscpn example
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main(void)
{
clrscr();
char *str1 = "Rahul";
char *str2 = "rahul";
int len;
len = strcspn(str1, str2);
cout<<"Character where strings different is at position: "<<len;
getch();
}
OUTPUT
Character where strings different is at position: 1
C++ strlwr example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str= "RAHUL SINGH";
char *str_lwr;
//convert string into lower case by strlwr
str_lwr = strlwr(str);
cout<<str_lwr;
getch();
}
OUTPUT
rahul singh
C++ strupr example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str= "rahul singh";
char *str_upr;
//convert string into upper case by strupr
str_upr = strupr(str);
cout<<str_upr;
getch();
}
OUTPUT
RAHUL SINGH
C++ strchr example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str1 = "Rahul singh";
char *str2, c = 'i';
//scan str1 in the forward direction for a character i
str2= strchr(str1,c);
if(str2)
cout<<"Character "<<c<<" Found at positition: "<<str2-str1;
else if(!str2)
cout<<"Not Found";
getch();
}
OUTPUT
Character i Found at position: 7
C++ strrchr example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str1 = "Rahul singh";
char *str2, c = 'i';
//scan str1 in the reverse direction for a character i
str2= strrchr(str1,c);
if(str2)
cout<<"Character "<<c<<" Found at postition: "<<str2-str1;
else if(!str2)
cout<<"Not Found";
getch();
}
OUTPUT
Charater i Found at position: 7
C++ strdup example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str= "rahul", *dup_str;
//copy str into newly created location dup_str
dup_str = strdup(str);
cout<<dup_str;
// freeing the space allocated by strdup
delete dup_str;
getch();
}
OUTPUT
rahul
C++ strrev example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str = "rahul singh";
cout<<"Before reverse: "<<str<<endl;
//reverse a string by strrev
strrev(str);
cout<<"After reverse : "<<str;
getch();
}
OUTPUT
Before reverse: Rahul Singh
After reverse : hginS luhaR
C++ strstr example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str1 = "Rahul singh";
char *str2 = "sin";
//scan the str1 in the reverse direction for substring str2 by strstr
str2= strstr(str1,str2);
if(str2)
cout<<"string "<<str2<<" Found at postition: "<<str2-str1;
else if(!str2)
cout<<"Not Found";
getch();
}
OUTPUT
string sin Found at position: 6
आइए अब इनमे से किसी एक function को real world के साथ प्रयोग करते हैं-
real world example of strcmp in C++
यह program कुछ अलग तरीके से बनाया गया है। program एक login system की तरह व्यवहार करता है, जिसमें user से input के रूप में password मांगा जाता है।
इसमें, हमने दोनों password (एक तरह string )को strcmp
से compare (एक जो user input होगा और दूसरा program में declared है ) किया है –
#include<iostream.h>
#include<conio.h>
#include<stdlib.h> //getline)(
#include <string.h>
void main()
{
start: // goto statement used
clrscr();
char psd[10];
char pwd[10]="password";
cout<<"Enter Password: ";
cin.getline(psd,10);
if(strcmp(psd,pwd)==0)
{
cout<<"Matched..\n\n\t";
system("pause"); // pause display screen with a message
} else {
cout<<"Try again..";
goto start; // transfer control if condition false
}
}
OUTPUT
Enter Password: password
Matched..
press any key to continue.
difference between C-string and C++ string in hindi
C-string or C++ character array | C++ string |
---|---|
c -string एक character array का रूप है, जो char data type का sequence होता है। इसलिए इसे character array भी कहा जाता है। | C ++ string एक class है जो ANSI द्वारा C ++ में जोड़ा गया हैं |
C- string का declaration इस प्रकार है,
जहाँ char एक data type है तथा str एक identifier. |
जहाँ char data type है और str एक identifier है,
जहां string एक class है, जो पहले से ही C ++ standard library में pre – defined है और str, string class object है। |
C-string का initialization इस प्रकार होगा।
|
while C++ string will be as follows,
|
array की तरह, यह single character को access करने के लिए index value का use होता है। कोई pre -defined function की आवश्यकता नहीं होती।
|
string से single character को access करने के इसका pre -defined function है।
|
क्योंकि यह एक array का रूप है और array एक derived data type होता है। तो यह भी एक derived data type होगा । | string एक class है और class एक user defined data type-प्रकार है लेकिन यह नहीं है। हालाँकि, ये built -in data type की तरह व्यवहार करते हैं। जैसा कि ऊपर दिया गया है। |
standard operator का प्रयोग c -string के साथ नहीं होता।
|
C++ string standard operator प्रयोग कर सकते हैं।-
|
चूंकि character array operators को support नहीं करता इसलिए यह copy और swap जैसे operation के लिए pre -defined function का उपयोग करता है-
हालांकि, string के साथ pointer का use करके assignment operator का कुछ हद तक प्रयोग किया जा सकता है-
are possible. |
यह इसके लिए pre -defined function हैं –
और क्योंकि यह operators को support करता है, इसलिए
are also possible. |
character array, String Constant Pool memory location में store होता है जो heap memory का एक हिस्सा होता है । | string , heap में store होता है। |
previous – storage classes in C++ in hindi
next – OOPs Concept in C++ in hindi