String class member functions

string a;

a.size() or a.length();              //       to find the length of string.

reverse(a.begin(),a.end());    //        to reverse the string

a.swap(b)         //        to swap string a with b.



To enter a multi word string like"my name is ABC";

string s;
getline(cin,s);
and it can simply be printed on screen using cout.

To enter a multiple line string;

#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    string s,a;
    cin>>t;
    while(t--)
{
 cin>>a ;
 s+=a+"\n";     // this (\n) make a multi line string.
}
   cout<<s;
}

INPUT 1:
5
XO|XX
XX|XX
XO|OX
XO|OO
OX|XO

OUTPUT 1:
XO|XX
XX|XX
XO|OX
XO|OO
OX|XO

INPUT 2:
3
RAM Rajesh Ramesh

OUTPUT 2:
RAM
Rajesh 
Ramesh

To find the position of character and replace it with other;

string st_name;
st_name.find("character/ multiple adjacent characters");
//this predefined function returns the first occurred position of the searched element.
Note:The index of the first character is 0 (not 1).
st_name.replace(position, len, str);
// position -- It is the starting insertion point,
  len= the length upto which the insertion is to be done,
  str= string object or value that is is to be placed;



#include<bits/stdc++.h>
using namespace std;
int main() 
    string s = "The world is so beautiful"; 
string a = "hi, I am your friend";
    int pos =s.find("so");
    s.replace(pos  , 2 , "gm" );
    cout<<a.find('h')<<" "<<a.find("hi")<<" "<<a.find('i')<<" "<<a.find(" ")<<endl;
    cout<<s<<endl<<pos;
   //position of searched element is also displayed for better understanding.
    return 0; 

OUTPUT :
0 0 1 3
The world is gm beautiful
13

NOTE    positions are allocated starting from (0) and for a multi word string the new line character 
i,e;  \n also occupies one position.
EX-
#include <bits/stdc++.h>
using namespace std;
int main() {
    int t,pos;
    string s,a;
    cin>>t;
    while(t--) cin>>a , s+=a+"\n"; // contructs a multiple line string.
       cout<<s.find("XO")<<endl<<s.find("OO");
} 

INPUT:
5
XO|XX
XX|XX
XO|OX
XO|OO
OX|XO

OUTPUT:
0
21



To convert a character of string to integer.
#include<bits/stdc++.h>
#include<sstream>
using namespace std;
int main()
{
int a,b=2;
string s; char c;
cin>>s;                  
c=s[3];            //Lets see if we can convert the character at index 3 to integer,
a=s[3]-48;     // 48 is subtracted because integers lag characters in ASCII code by 48;
Or
a=s[3]-'0';
Or
istringstream(c)>>a; //this predefined function converts string or character into int.
cout<<a+b;      //we added another int. to (a) to make sure that (a) is converted to int., because int can only be added to int.
}

INPUT:
123454

OUTPUT:
6










Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree