#include<iostream>
#include<string>
int main()
{
std::string greeting ="hello "; //greeting is an object of class string.
std::cout<<greeting[0]<<std::endl;
//default value for string is empty string;
using std::string;
using std::cout;
using std::endl;
string name;
cout<<name<<endl; //empty string is "", with nothing in between.
//we can add strings;
name=greeting+"vishal rao";
cout<<name<<endl;
//we can also append to string;
name+='!';
cout<<name<<endl;
//length will provide length of string, here function is called as if name is object of class string
//and lenght is function for whole class.
cout<<name.length()<<endl;
char new_name[]="virat kohli"; //now since new_name has assigned memory of 11 characters, we can't store larger/smaller string in here, and that is the limitation of this method.
//whereas we can use string class in more flexible way.
/*How to take input as string.
string var;
cout<<"Enter some string with space";
std::cin>>var;
cout<<var<<endl;//note that cin only catches the first word, it does not catche
//only first word gets stored in string.
string left_over;
std::cin>>left_over;
cout<<left_over<<endl; //this will automatically grab the second word which was left over.
*/
// to get the whole sentence, we use the function getline();
string line1;
cout<<"enter some sentence:"<<endl;
getline(std::cin,line1);
cout<<line1<<endl;
/*string methods or Member functions:
1.string_obj.length()
2.string_obj.size()
modifier methods:
1. string_obj+="there"; this will modify the string_object.
2. string_obj.append("there!")
3. string_obj.insert(3," ") //it will insert the space after 3 characters of string_obj
4. string_obj.erase(3,Number of characters to remove)
string_obj.erase(3) will remove every character after 3 characters of string obj
string_obj.erase(string_obj.length()-1) will get rid of last character. or use string_obj.pop_back() will do it.
5. string_obj.replace(starting index,length,"string to be in place");
6. string_obj.find("string to be found"); will return the starting index.
we can sensor some string by this method:
string_obj.replace(string_obj.find("word to be sensored"),string_obj.sizeof(),"******");
string line="hi";
line*=4;
cout<<line<<endl; will throw error, we can't multiply string with integer.
7. string_obj.substr(starting index, no of characters)
8. string_obj.find_first_of("string/char to be found"); it will return npos i.e. unsigned x=-1 value; if
character is absent in string. it is called npos which is set as -1.
it is used for searching of characters:
if(string_obj.find_first_of("!")==-1) cout<<"NOT FOUND";
if(string_obj=="whatever") cout<<"equal";
we can also use compare method to compare strings
if(string_obj.compare("whatever")==0) cout<<"equals";
//returns zero if two strings are equal.
//in java we have to use compare method
*/
}