इस खंड में, हम देखेंगे कि C++ का उपयोग करके किसी स्ट्रिंग को बहुत तेज़ी से कैसे उलटना है। रिवर्सिंग के लिए एल्गोरिथम लाइब्रेरी में एक बिल्ट-इन फंक्शन होता है, जिसे रिवर्स () कहा जाता है। यह फ़ंक्शन एक कंटेनर की शुरुआत और समाप्ति सूचक लेता है, फिर तत्वों को उलट देता है।
Input: A number string “Hello World” Output: “dlroW olleH”
एल्गोरिदम
Step 1:Take a string Step 2: reverse it using reverse() function Step 3: Print the result. Step 4: End
उदाहरण कोड
#include<iostream>
#include<algorithm>
using namespace std;
main() {
string my_str = "This is a string to be reversed";
cout << "Initial state of this string: " << my_str << endl;
//reverse using the reverse() function for reversing a string
reverse(my_str.begin(), my_str.end());
cout << "Final state of this string: " << my_str;
} आउटपुट
Initial state of this string: This is a string to be reversed Final state of this string: desrever eb ot gnirts a si sihT