DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

Modifiers in C++

To concatenate two string we use append modifier in C++ .

`#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
string s = "Hello World!!";
string s1 = "This is MJ" ;
s.append(s1) ;
cout << s << endl ;
}`
Enter fullscreen mode Exit fullscreen mode

Output -
Hello World!!This is MJ

For efficiency we can use s += s1 ;

`#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
string s = "Hello World!!";
string s1 = "This is MJ" ;
s += s1 ;
cout << s << endl ;
}`

Enter fullscreen mode Exit fullscreen mode

Output-
Hello World!!This is MJ

push_back function is used to add a alphabet in the last of the string .

`#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
string s = "Hello World!!";
string s1 = "This is MJ" ;
s1.push_back('T') ;
cout << s1 << endl ;

} 
`
Enter fullscreen mode Exit fullscreen mode

Output -
This is MJT

For efficiency we can use s1 += 'F' ;
`

`

include

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s = "Hello World!!";
string s1 = "This is MJ";
s1 += "ft";
cout << s1 << endl;
}
`
`

pop_back() is used to delete a alphabet at the last of the string .

`

`

include

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s = "Hello World!!";
string s1 = "This is MJ";
s1.pop_back() ;
cout << s1 << endl;
}
`
`
assign() is used to replace current string .

`#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s = "Hello World!!";

    s.assign("MU") ;
    cout << s << endl;
}`
Enter fullscreen mode Exit fullscreen mode

We can do it like this as well -
`

`

include

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s = "Hello World!!";

s = "MJ";
cout << s << endl;
Enter fullscreen mode Exit fullscreen mode

}
`

s.erase() is used to delete index of string . We have to write positon for this .

`

include

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s = "Hello World!!";

s.erase(5) ;
cout << s << endl;
Enter fullscreen mode Exit fullscreen mode

}
`
`
Input - Hello World
Output - Hello
If we want to delete some index . We have to write index number and number of alphabet we want to delete

`#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s = "Hello World!!";

    s.erase(5,2) ;
    cout << s << endl;
}
Enter fullscreen mode Exit fullscreen mode


To replace the word with other word by telling index number -

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s = "Hello World!!";

    s.replace(5,7," Bangladesh") ;
    cout << s << endl;
}
Enter fullscreen mode Exit fullscreen mode

`

Top comments (0)