Strings in C++
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
int main() {
// Write C++ code here
string txt="Gunjan";
txt.append(" Nayak");
cout<<txt<<endl;
cout<<txt.length()<<endl;
cout<<txt.size()<<endl;
cout<<"Character at index 7 is "<<txt.at(7)<<endl;
cout<<"Substring from 6 to 12 is \""<<txt.substr(5,12)<<"\""<<endl;
// int position=txt.find("Nayak");
size_t position=txt.find("Nayak");
cout<<position<<endl;
// Modifying a String from a range:-
txt.replace(7,12,"Kumar Nayak");
cout<<txt<<endl;
// Erasing Characters in a string:-
txt.erase(5,7); // first argument determines the starting position and the second argument determines the number of characters to be erased/deleted.
cout<<txt<<endl;
return 0;
}
Comments
Post a Comment