String manipulations in C++ In Hindi
C++ प्रोग्रामिंग लैंग्वेज में, स्ट्रिंग्स डाटा टाइप को मुख्य रूप से दो तरीको से हैंडल किया जाता हैं. C++ प्रोग्राम में C-स्टाइल स्ट्रिंग्स कैरेक्टर ऐरे एक स्ट्रिंग डाटा टाइप डीलिंग लाइब्रेरी है, जिसे हमने पहले एनालाइज किया था। std::string, यह C++ प्रोग्रामिंग में स्टैंडर्ड लाइब्रेरी का एक बिल्ट-इन फंक्शन या फीचर्स है. जो C++ प्रोग्राम में स्ट्रिंग्स डाटा टाइप मेथड के लिए एक हायर-लेवल डाटा इनफार्मेशन एब्स्ट्रैक्शन मेथड को प्रोवाइड करता है।

एडवांस C++ प्रोग्रामिंग में, std::string स्ट्रिंग लाइब्रेरी में इजी, सेफ्टी, और बिल्ट-इन फंक्शन्स की वजह से स्ट्रिंग मैनिपुलेशन के लिए डिजायर ऑप्शन है। यह फीचर्स C++ प्रोग्रामिंग डेवलपमेंट में मुख्य रूप से std::string को हैंडल और मैनेज करने पर फोकस करेगा।
Basic Operations with std::string in C++ Programming.
C++ प्रोग्राम में std::string क्लास लाइब्रेरी स्ट्रिंग्स को हैंडल और मैनेज करने के लिए कई तरह के बिल्ट-इन फंक्शन्स और फीचर्स प्रोवाइड करता है. C++ यूजर इन क्लास लाइब्रेरी फंक्शन का यूज़ कर एक मल्टीप्ल स्ट्रिंग करैक्टर टास्क को परफॉर्म कर सकते है।
So, let’s take a closer look at some common std::string class library operations.
Declaring and initializing a std::string in C++.
C++ यूजर std::string क्लास लाइब्रेरी में डिजायर स्ट्रिंग करैक्टर डाटा टाइप एलिमेंट को इन तरीकों से डिक्लेयर और इनिशियलाइज़ कर सकते हैं.
Example of Declaring and initializing a std::string.
#include <iostream>
#include <string> // here we use std::string for string data type operation in c++
int main() {
std::string text1 = “Vcanhelpsu, Edtech”; // here we declare a string literal data type
std::string text2 = text1; // here we Copying another string into
std::string text3 = text1;
std::cout << text1 << std::endl; // Result is – Vcanhelpsu, Edtech
std::cout << text2 << std::endl; // Result is – Vcanhelpsu, Edtech
std::cout << text3 << std::endl; // Result is – Vcanhelpsu, Edtech
return 0;
}
std::string class library basic operations in C++ program.
String length (size() or length()) concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में size() या length() बिल्ट-इन स्ट्रिंग फ़ंक्शन को अप्लाई करके std::string की मौजूदा लेंथ को फाइंड कर सकते हैं। जिसमे हम दोनों यूजर डिफाइन डिक्लेअर स्ट्रिंग डाटा टाइप एलिमेंट इक्विलिटी साइज को एनालाइज कर सकते हैं।
Example of String length (size() or length()).
#include <iostream>
#include <string>
int main() {
std::string c_name = “Vcanhelpsu Edtech”;
std::cout << “Size of company name text – ” << c_name.size() << std::endl; // Result – 17
std::cout << “Length of company name string – ” << c_name.length() << std::endl; // Result – 17
return 0;
}
Accessing characters concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में इंडेक्सिंग या at() फ़ंक्शन को अप्लाई करके किसी यूजर डिक्लेअर स्ट्रिंग डाटा टाइप में अलग-अलग इंडिविजुअल कैरेक्टर्स को एक्सेस और डिस्प्ले कर सकते हैं। किसी C++ प्रोग्राम में at() फ़ंक्शन मोर एफ्फिसिएंट और सिक्योर है. क्योंकि यह यूजर डिफाइन स्ट्रिंग बाउंड्स को चेक करता है।
Example of Accessing characters concept of the std::string class library.
#include <iostream>
#include <string>
int main() {
std::string c_name = “Vcanhelpsu”;
// here we Access character of string using storage element indexing method
std::cout << “First character of string – ” << c_name[0] << std::endl; // Result is – V
// here we Access character of string using the at() function bounds checked method
std::cout << “Second character of string – ” << c_name.at(1) << std::endl; // Result is – c
return 0;
}
Modifying a string concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में नई वैल्यू को असाइन करके या मौजूदा स्ट्रिंग में नई एलिमेंट जोड़कर या मौजूदा स्ट्रिंग के कैरेक्टर को अपडेट या रिप्लेस कर स्ट्रिंग को मॉडिफ़ाई कर सकते हैं।
Assigning a new string.
std::string c_name = “Vcanhelpsu”;
c_name = “Edtech”; // अब यहाँ c_name स्ट्रिंग वेरिएबल में “Edtech” टेक्स्ट वैल्यू को होल्ड है.
Adding string elements using the += or append() function.
std::string c_name = “Vcanhelpsu”;
c_name += ” Edtech”; // Add using the `+=` operator.
std::cout << c_name << std::endl; // Result – Vcanhelpsu Edtech
c_name.append(“#”);
std::cout << c_name << std::endl; // Result – Vcanhelpsu Edtech#
Replacing string characters at a specific position.
std::string c_name = “Vcanhelpsu”;
c_name[0] = ‘V’; // Displays the first character here as ‘V’.
std::cout << c_name << std::endl; // Result – Vcanhelpsu
Replacing a substring in the std::string class library using the replace() function.
#include <iostream>
#include <string>
int main() {
std::string text = “Vcanhelpsu, Platform”;
// here we Replace substring index 10 to 11 with a new user define string
text.replace(10, 11, ” Edtech”);
std::cout << text << std::endl; // Result – Vcanhelpsu Edtech
return 0;
}
String concatenation concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में + ऑपरेटर या append() बिल्ट-इन फंक्शन मेथड को अप्लाई करके दो अलग अलग स्ट्रिंग टेक्स्ट डाटा को कॉन्कैटन कर सकते हैं।
Example of String concatenation concept of the std::string class library.
#include <iostream>
#include <string>
int main() {
std::string text1 = “Vcanhelpsu”;
std::string text2 = “Edtech”;
// here it Concatenate text with ‘+’ opeator to add two different string
std::string output = text1 + ” ” + text2;
std::cout << output << std::endl; // Result – Vcanhelpsu Edtech
// here it Concatenate text with ‘append()’ operator to add two different string
text1.append(” “).append(text2);
std::cout << text1 << std::endl; // Result – Vcanhelpsu Edtech
return 0;
}
String comparison concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में कम्पेरिजन ऑपरेटर्स या compare() बिल्ट-इन फंक्शन मेथड को अप्लाई करके दो अलग अलग स्ट्रिंग्स की तुलना कर सकते हैं।
Example of String comparison concept of the std::string class library.
#include <iostream>
#include <string>
int main() {
std::string text1 = “Bhavishi”;
std::string text2 = “Siddhi”;
if (text1 == text2) {
std::cout << “Both text Strings are equal.” << std::endl;
} else {
std::cout << “Both text Strings are different.” << std::endl; // Result – Both text Strings are different.
}
return 0;
}
The compare() function concept of the std::string class library.
- C++ यूजर किसी प्रोग्राम में compare() फ़ंक्शन कुछ बिल्ट-इन फीचर्स प्रोवाइड करता है.
- यदि यूजर डिफाइन दोनों स्ट्रिंग टेक्स्ट बराबर हैं. तो आउटपुट 0 होता है.
- यदि यूजर डिफाइन फर्स्ट स्ट्रिंग सेकंड स्ट्रिंग से लेक्सिकोग्राफ़िकली छोटी है. तो यह अपने आप में एक नेगेटिव वैल्यू है.
- यदि यूजर डिफाइन फर्स्ट स्ट्रिंग सेकंड से लेक्सिकोग्राफ़िकली बड़ी है, तो पॉज़िटिव वैल्यू वैल्यू है।
Example of compare() function concept.
#include <iostream>
#include <string>
int main() {
std::string text1 = “Java”;
std::string text2 = “Python”;
if (text1.compare(text2) == 0) {
std::cout << “Here both text Strings are equal in nature.” << std::endl;
} else if (text1.compare(text2) < 0) {
std::cout << “Here text1 is lexicographically string order less than text2 string.” << std::endl;
} else {
std::cout << “Here text1 is lexicographically string order greater than text2.” << std::endl;
}
return 0;
}
Substring concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में substr() फ़ंक्शन को अप्लाई करके किसी बड़ी टेक्स्ट स्ट्रिंग से एक छोटी सबस्ट्रिंग एक्सट्रेक्ट कर सकते हैं.
You must have two required parameters for this.
- शुरुआती स्ट्रिंग जगह (इंडेक्स).
- मौजूदा सबस्ट्रिंग की लंबाई (ऑप्शनल).
Example of Substring is a concept.
#include <iostream>
#include <string>
int main() {
std::string c_name = “Vcanhelpsu, Edtech”;
// here it Extract substring text starting from position 11 with length 8
std::string subtext = c_name.substr(11, 8);
std::cout << “Here Substring text of c_name – ” << subtext << std::endl; // Result – Here Substring text of c_name – Edtech
return 0;
}
String search concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में find() या rfind() फंक्शन मेथड को अप्लाई करके मौजूदा स्ट्रिंग में सबस्ट्रिंग या कैरेक्टर को सर्च कर सकते हैं।
Example of finding a character or substring.
#include <iostream>
#include <string>
int main() {
std::string c_name = “Vcanhelpsu, Edtech”;
// here we Find the first text string occurrence of ‘p’
size_t loc = c_name.find(‘p’);
if (loc != std::string::npos) {
std::cout << “‘p’ character found at location – ” << loc << std::endl; // Result – 7
} else {
std::cout << “‘p’ character not found -” << std::endl;
}
// here it Find a substring in complete above string
loc = c_name.find(“Edtech”);
if (loc != std::string::npos) {
std::cout << “‘Edtech’ word found at location – ” << loc << std::endl; // Result – 12
}
return 0;
}
Explanation of finding a character or substring.
- यहाँ इस प्रोग्राम में find() फंक्शन मौजूदा सबस्ट्रिंग या कैरेक्टर के पहली बार आने की करंट पोजीशन लोकेशन को इंडीकेट करता है।
- यहाँ इस एक्साम्प्ल में npos एक कॉन्स्टेंट है. जो मौजूदा प्रोग्राम में “‘Edtech’ word found at location” इनफार्मेशन को डिस्प्ले करता है।
Finding the last occurrence (rfind()) function.
#include <iostream>
#include <string>
int main() {
std::string text = “Java, Python Matlab”;
// here it Find the last occurrence of “Matlab” text string
size_t loc = text.rfind(“Matlab”);
if (loc != std::string::npos) {
std::cout << “Here ‘Matlab’ text found at location – ” << loc << std::endl; // Result – 13
}
return 0;
}
String input/output concept of the std::string class library.
C++ यूजर किसी प्रोग्राम में मौजूदा टेक्स्ट स्ट्रिंग को रीड और प्रिंट करने के लिए std::cin और std::cout स्टेटमेंट मेथड का यूज़ कर सकते हैं।
Example of reading a String input/output.
#include <iostream>
#include <string>
int main() {
std::string message;
std::cout << “Enter a desire text message – “;
std::getline(std::cin, message); // here it Reads the entire text line, including white spaces
std::cout << “Your input text message is – ” << message << std::endl;
return 0;
}
Explanation of reading a String input/output.
- यहाँ इस प्रोगाम में std::getline() लाइब्रेरी फंक्शन स्पेस सहित पूरी टेक्स्ट लाइन को रीड करता है।
- यहाँ std::cin >> message; पहले यूजर द्वारा एंटर एम्प्टी स्पेस तक इनपुट को रीड करता है. जिसमे अधिक स्पेस वाली टेक्स्ट स्ट्रिंग के लिए इसे आप यूज़ नहीं कर सकते है।
