« Strings in C++
Introduction to strings in C++
1#include <iostream>2#include <iomanip>3#include <algorithm>4#include <string>5#include <cstring>6#include <vector>7#include <cmath>8#include <map>9#include <climits>10// climits for INT_MIN11#include <unordered_map>12using namespace std;13int main()14{15 string *sp = new string;16 // dynamic allocation of string1718 *sp = "India";19 cout << "*sp: " << *sp << endl;20 cout << "sp: " << sp << endl;2122 string s = "abc";23 // static allocation of string2425 // cin >> s;26 // I Love India will fail here as space is there. cin breaks around space2728 getline(cin, s);29 cout << s << endl;3031 s = "Geek Convert";32 cout << s << endl;33 s[0] = 'r';34 // strings can be treated as array. We can replace its char by char3536 cout << s << endl;3738 string s1 = "abc";39 cout << s1 << endl;4041 string s2 = s + s1;42 // we can concatenate strings4344 cout << s2 << endl;4546 cout << s2.size() << endl;47 cout << s2.length() << endl;4849 cout << s2.substr(10) << endl;50 // gets everything after 105152 cout << s2.substr(10, 3) << endl;53 // gets everything after 10 but only length 35455 cout << s2.find("ek") << endl; // returns position for the first occurrence of an in the string s25657 return 0;58}