fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. string str1("first string");
  7.  
  8. string str2(str1);
  9.  
  10. string str3(5, '#');
  11.  
  12. string str4(str1, 6, 6);
  13.  
  14. string str5(str2.begin(), str2.begin() + 5);
  15.  
  16. cout << str1 << endl;
  17. cout << str2 << endl;
  18. cout << str3 << endl;
  19. cout << str4 << endl;
  20. cout << str5 << endl;
  21.  
  22. string str6 = str4;
  23.  
  24. str4.clear();
  25.  
  26. int len = str6.length();
  27.  
  28. cout << "Length of string is " << len << endl;
  29.  
  30. char ch = str6.at(2);
  31.  
  32. cout << "Third character of string is: " << ch << endl;
  33.  
  34. char ch_f = str6.front();
  35. char ch_b = str6.back();
  36.  
  37. cout << "First char is : " << ch_f << ", Last char is :" << ch_b << endl;
  38.  
  39. const char* charstr = str6.c_str();
  40. printf("%s\n", charstr);
  41.  
  42. str6.append("Extension");
  43.  
  44. str4.append(str6, 6, 6);
  45.  
  46. cout << str6 << endl;
  47. cout << str4 << endl;
  48.  
  49. if (str6.find(str4) != string::npos)
  50. cout << "str4 found in str6 at " << str6.find(str4) << " pos" << endl;
  51. else
  52. cout << "str4 not found in str6" << endl;
  53.  
  54. cout << str6.substr(7,3) << endl;
  55.  
  56. cout << str6.substr(7) << endl;
  57.  
  58. str6.erase(7,4);
  59. cout << str6 << endl;
  60.  
  61. str6.erase(str6.begin() + 5, str6.end() - 3);
  62. cout << str6 << endl;
  63.  
  64. str6 = "This is an example";
  65.  
  66. str6.replace(2,7, "ese are test");
  67.  
  68. cout << str6 << endl;
  69.  
  70. return 0;
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5388KB
stdin
Standard input is empty
stdout
first string
first string
#####
string
first
Length of string is 6
Third character of string is: r
First char is : s, Last char is :g
string
stringExtension
Extens
str4 found in str6 at 6 pos
xte
xtension
stringEsion
strinion
These are testn example