×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Strings | Find output programs | Set 5

This section contains the C++ find output programs with their explanations on C++ Strings (set 5).
Submitted by Nidhi, on July 18, 2020

Program 1:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "";

    str.push_back('i');
    str.push_back('n');
    str.push_back('c');
    str.push_back('l');
    str.push_back('u');
    str.push_back('d');
    str.push_back('e');
    str.push_back('h');
    str.push_back('e');
    str.push_back('l');
    str.push_back('p');

    for (int i = 0; i < str.length(); i++) {
        str.pop_back();
    }

    return 0;
}

Output:

Varies compiler to compiler.

Explanation:

The pop_back() is introduced in C++11 for string. And in the earlier versions of C++, it will generate the following error:

[Error] 'std::string' has no member named 'pop_back'

Program 2:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "";

    str.push_back('i');
    str.push_back('n');
    str.push_back('c');
    str.push_back('l');
    str.push_back('u');
    str.push_back('d');
    str.push_back('e');
    str.push_back('h');
    str.push_back('e');
    str.push_back('l');
    str.push_back('p');

    str.resize(7);

    cout << str << endl;

    return 0;
}

Output:

include

Explanation:

Here, we added string "includehelp" using push_back() function, then we changed the size of the string str using resize() member function and then print string str. So "include" will be printed on the console screen.

Program 3:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string STR = "IncludeHelp";

    std::string::iterator ITR;

    for (ITR = STR.begin(); ITR != STR.end(); ITR++)
        cout << *ITR;

    return 0;
}

Output:

IncludeHelp

Explanation:

In the above program we created string STR, which is initialized with "IncludeHelp", and we created an iterator ITR. Using iterator ITR we accessed each element of string from beginning to the end and print on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.