fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. // INITIALIZATION
  9.  
  10. string a; // Empty string
  11. string b("Quora");
  12. string c = "Quora";
  13. string d = b;
  14. string e(c);
  15.  
  16. // INITIALIZING WITH C-STYLE STRING
  17. char str[10] = "Quora";
  18. string f = str;
  19. string g(str);
  20.  
  21.  
  22. // CHECK STRING IS EMPTY OR NOT
  23. if (a.empty())
  24. cout << "Empty string\n";
  25.  
  26.  
  27. // FIND LENGTH OF STRING
  28. int bLen = b.length();
  29. // cout << bLen << endl;
  30.  
  31.  
  32. // CONCATENATION OF STRING
  33. string abc = b + d;
  34.  
  35. // Note : For concatenation to work, atleast one of them must be <string> type
  36. // string tN = "Does" + "not" + "work" // compile time error
  37.  
  38. string tY = string("This") + "will" + "work";
  39. // tY = "This will work"
  40.  
  41.  
  42.  
  43. // FIND A SUBSTRING
  44.  
  45. // string.find() -> finds first occurence
  46. // string.rfind() -> finds last occurence
  47.  
  48. int found = tY.find("will");
  49.  
  50. if (found == -1)
  51. cout << "Not Found\n";
  52. else
  53. cout << "Found at index : " << found << endl;
  54.  
  55.  
  56.  
  57. // MAKE THE STRING EMPTY
  58. // tY.clear();
  59.  
  60.  
  61. // ERASE ANY SUBSTRING
  62. // string.erase(index, length)
  63.  
  64. tY.erase(0, 5);
  65. // cout << tY << endl;
  66.  
  67.  
  68. // STRING TO SUBSTRING
  69. // string.substr(index, length)
  70.  
  71. string subString = b.substr(1, 3);
  72. // cout << subString << endl;
  73.  
  74.  
  75.  
  76. // INSERT A STRING
  77. // string.insert(index, string)
  78.  
  79. tY.insert(0, "Quora "); // inserts at beginning
  80. // cout << tY << endl;
  81.  
  82.  
  83. // COMPARE STRINGS
  84. // all relational operator work on string
  85. // == != > < >= <=
  86.  
  87. if (d == c)
  88. cout << "Equal strings\n";
  89.  
  90. // Alternatively
  91. // s1.compare(s2)
  92.  
  93.  
  94.  
  95. // Compatibility of <string> with C-style string
  96.  
  97. /********* string.c_str() behaves as C-style string *******/
  98.  
  99. int len = strlen(tY.c_str());
  100. cout << len << endl;
  101.  
  102. string number = "12345";
  103. int num = atoi(number.c_str());
  104. cout << num << endl;
  105.  
  106.  
  107. // Whenever char * is required rather than const char * USE
  108. // const_cast<char *> (string.c_str())
  109.  
  110. // Splitting a string based on a tokenizer
  111. // strtok(char *, const char *)
  112.  
  113. string sentence = "Today is Monday 29-07-14";
  114. char *p = strtok(const_cast<char *>(sentence.c_str()), " ");
  115. while (p != NULL) {
  116. cout << p << endl;
  117. p = strtok(NULL, " ");
  118. }
  119.  
  120. return 0;
  121. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Empty string
Found at index : 4
Equal strings
13
12345
Today
is
Monday
29-07-14