SHARE
TWEET

Everything about string

_Muhammad Jun 5th, 2020 (edited) 935 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///In the name of ALLAH
  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.     return 0;
  139. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top