#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
// INITIALIZATION
string a; // Empty string
string b("Quora");
string c = "Quora";
string d = b;
string e(c);
// INITIALIZING WITH C-STYLE STRING
char str[10] = "Quora";
string f = str;
string g(str);
// CHECK STRING IS EMPTY OR NOT
if (a.empty())
cout << "Empty string\n";
// FIND LENGTH OF STRING
int bLen = b.length();
// cout << bLen << endl;
// CONCATENATION OF STRING
string abc = b + d;
// Note : For concatenation to work, atleast one of them must be <string> type
// string tN = "Does" + "not" + "work" // compile time error
string tY = string("This") + "will" + "work";
// tY = "This will work"
// FIND A SUBSTRING
// string.find() -> finds first occurence
// string.rfind() -> finds last occurence
int found = tY.find("will");
if (found == -1)
cout << "Not Found\n";
else
cout << "Found at index : " << found << endl;
// MAKE THE STRING EMPTY
// tY.clear();
// ERASE ANY SUBSTRING
// string.erase(index, length)
tY.erase(0, 5);
// cout << tY << endl;
// STRING TO SUBSTRING
// string.substr(index, length)
string subString = b.substr(1, 3);
// cout << subString << endl;
// INSERT A STRING
// string.insert(index, string)
tY.insert(0, "Quora "); // inserts at beginning
// cout << tY << endl;
// COMPARE STRINGS
// all relational operator work on string
// == != > < >= <=
if (d == c)
cout << "Equal strings\n";
// Alternatively
// s1.compare(s2)
// Compatibility of <string> with C-style string
/********* string.c_str() behaves as C-style string *******/
int len = strlen(tY.c_str());
cout << len << endl;
string number = "12345";
int num = atoi(number.c_str());
cout << num << endl;
// Whenever char * is required rather than const char * USE
// const_cast<char *> (string.c_str())
// Splitting a string based on a tokenizer
// strtok(char *, const char *)
string sentence = "Today is Monday 29-07-14";
char *p = strtok(const_cast<char *>(sentence.c_str()), " ");
while (p != NULL) {
cout << p << endl;
p = strtok(NULL, " ");
}
return 0;
}