Advertisement
ANTAR_NANDI

STL String

Apr 5th, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | Software | 0 0
  1. ///In the name of Krisna
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. /// Declare string
  9. string s;
  10.  
  11. /// Assign string
  12. s = "abcdf";
  13.  
  14. /// Printing size of string
  15. cout << s.size() << endl; /// 5
  16.  
  17. /// Printing string
  18. cout << s << endl; /// abcdf
  19.  
  20. /// Pushing char back to a string
  21. s += 'b';
  22. s += 'c';
  23. cout << s << endl; /// abcdfbc
  24.  
  25. /// Taking input string
  26. cin >> s;
  27. cout << s << endl;
  28.  
  29. s = "asdfgg";
  30.  
  31. /// Checking is a string empty or not
  32. string s1;
  33. cout << s.empty() << endl; /// 0
  34. cout << s1.empty() << endl; /// 1
  35.  
  36. /// Assigning an string in another string variable
  37. s1 = s;
  38. s.clear();
  39.  
  40. cout << s.empty() << endl; /// 1
  41. cout << s1.empty() << endl; /// 0
  42.  
  43. /// assigning 'k' in 0-th index
  44. s = "asdfg";
  45. s[0] = 'k';
  46. cout << s << endl; ///kasdfg;
  47.  
  48. s = "abc";
  49. s1 = "def";
  50.  
  51. /// String concatenation
  52. string tmp = s + s1;
  53. cout << tmp << endl; /// abcdef
  54.  
  55. /// String iterator
  56. string::iterator it;
  57. for ( it = s.begin(); it != s.end(); it++ ) cout << *it; /// abc
  58. cout << endl;
  59.  
  60. /// For each loop
  61. for ( auto c : s ) cout << c; /// abc
  62. cout << endl;
  63.  
  64.  
  65. s = "asd";
  66. tmp = s;
  67.  
  68. /// Comparing two strings
  69. if ( tmp == s ) cout << "Yes Match\n";
  70. else "No Match\n";
  71.  
  72. /// String reverse and checking is a string is palindrome or not
  73. s = "asddsa";
  74. tmp = s;
  75. reverse( tmp.begin(), tmp.end() );
  76.  
  77. if ( tmp == s ) cout << "Yes Palindrome" << endl;
  78. else cout << "Not Palindrome" << endl;
  79.  
  80. /// String sorting in non-decreasing order
  81. s = "gfds";
  82. sort ( s.begin(), s.end() );
  83. cout << s << endl; /// dfgs
  84.  
  85. /// String sorting in non-increasing order
  86. sort ( s.rbegin(), s.rend() );
  87. cout << s << endl; /// sgfd
  88.  
  89. /// Getting all unique elements of a string. Be care full, string should be sorted.
  90. s = "aaadddsss";
  91. int n = unique( s.begin(), s.end() ) - s.begin();
  92. for ( int i = 0; i < n; i++ ) cout << s[i];/// ads
  93. cout << endl;
  94.  
  95. /// Getting maximum element of string
  96. cout << *max_element( s.begin(), s.end() ) << endl; /// s
  97. /// Getting minimum element of string
  98. cout << *min_element( s.begin(), s.end() ) << endl; /// a
  99.  
  100. /// When we want to take input with space
  101. /// input : Muhammad Shahriar Alam
  102.  
  103. char c;
  104. cin >> c;
  105. getline( cin, s );
  106. s = c + s;
  107.  
  108. cout << s << endl; /// Muhammad Shahriar Alam
  109.  
  110. /// If we need to sort some string on lexicographical order :
  111.  
  112. vector<string> v;
  113. v.push_back( "Muhammad" );
  114. v.push_back( "Nova" );
  115. v.push_back( "Maslenia Mubarrat" );
  116. v.push_back( "CPS Academy" );
  117. v.push_back( "Rashedul Alam Anik" );
  118. v.push_back( "Farhan sadik Sakib" );
  119. v.push_back( "Gazi Mohaimin Iqbal" );
  120.  
  121. sort ( v.begin(), v.end() );
  122. for ( auto u : v ) cout << u << endl;
  123.  
  124. /**
  125.  
  126. Out put :
  127.  
  128. CPS Academy
  129. Farhan sadik Sakib
  130. Gazi Mohaimin Iqbal
  131. Maslenia Mubarrat
  132. Muhammad
  133. Nova
  134. Rashedul Alam Anik
  135.  
  136. */
  137.  
  138. s = "asdf";
  139.  
  140. s.pop_back(); /// removes last char of string
  141. cout << s.back() << endl; /// print last char of string
  142.  
  143. v.clear();
  144.  
  145. v = { "Shahriar", "Shahriar", "Momo", "Momo", "Sharif", "Sharif" };
  146. int Sz = unique ( v.begin(), v.end() ) - v.begin();
  147.  
  148.  
  149. cout << Sz << endl; /// Number of unique strings in vector v;
  150. for ( int i = 0; i < Sz; i++ ) cout << v[i] << endl; /// Prints all unique strings in vector v
  151.  
  152. /// Converting int to string
  153. int a = 123;
  154. s = to_string (a);
  155. cout << s << endl; /// 123
  156. s[0] = '3';
  157. cout << s << endl; /// 323
  158.  
  159. /// Converting string to integer
  160.  
  161. s = "123";
  162. a = stoi ( s );
  163. cout << a << endl; /// 123
  164. a++;
  165. cout << a << endl; /// 124;
  166.  
  167. /// Deleting a substring from string
  168.  
  169. s = "ShaKAKAhriar";
  170.  
  171. s.erase ( s.begin()+3, s.begin()+7 ); /// erase substring "KAKA" from string s
  172. cout << s << endl;
  173.  
  174.  
  175. /// Copying a substring of a string to a string
  176. tmp = "Gagha Alam Gadha";
  177. s = "Shahriar ";
  178.  
  179. copy ( tmp.begin()+6, tmp.begin()+10, back_inserter ( s ) ); /// copying "Alam substring to string s back.
  180. cout << s << endl; /// Shahriar Alam
  181.  
  182. /// Erasing all occurrence of a specific char from string.
  183.  
  184. s = "aaassdddaaasdd";
  185. s.erase ( remove ( s.begin(), s.end(), 'a' ), s.end() ); /// removes all 'a' from s
  186. cout << s << endl;
  187.  
  188. /// Checking is a string is substring of another string in O(n*m)
  189. s = "ashshasdakks";
  190.  
  191. if ( s.find( "asd" ) != -1 ) cout << "Substring found";
  192. else cout << "Not found";
  193.  
  194.  
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement