Q:

C++ Strings | Find output programs | Set 5

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Strings (set 5).

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;
}

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;
}

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;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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'

Answer Program 2:

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.

Answer Program 3:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Class and Objects | Find output programs | Set... >>
<< C++ Strings | Find output programs | Set 4...