Q:

C++ Strings | Find output programs | Set 4

belongs to collection: C++ find output programs

0

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

Program 1:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "includehelp";

    for (int i = 0; i < str.length(); i++) {
        cout << str.at(i) - 32 << " ";
    }

    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');

    for (int i = 0; i < str.length(); i++) {
        cout << str[i] << " ";
    }

    return 0;
}

Program 3:

#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_front('h');
    str.push_front('e');
    str.push_front('l');
    str.push_front('p');

    for (int i = 0; i < str.length(); i++) {
        cout << str[i] << " ";
    }

    return 0;
}

All Answers

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

Answer Program 1:

Output:

73 78 67 76 85 68 69 72 69 76 80

Explanation:

Here, we created a string str initialized with "includehelp" and we create a character variable ch.

Here, we accessed character index-wise from string str, and we subtract each character by 32. Then

The subtracted ASCII value will print on the console screen, because ASCII value of 'i' is 105 and after subtraction value will be 73, similarly each individual's ASCII value is subtracted by 32 and print on the console screen.

Answer Program 2:

Output:

i n c l u d e h e l p

Explanation:

Here, we created the string object str. We used push_back() member function of string class to insert the item into the string, here we added "i n c l u d e h e l p" string using push_back() function and print string character-wise using the for loop on the console screen.

Answer Program 3:

Output:

main.cpp: In function ‘int main()’:
main.cpp:17:9: error: ‘std::string {aka class std::basic_string}’ 
has no member named ‘push_front’; did you mean ‘push_back’?
     str.push_front('h');
         ^~~~~~~~~~
main.cpp:18:9: error: ‘std::string {aka class std::basic_string}’ 
has no member named ‘push_front’; did you mean ‘push_back’?
     str.push_front('e');
         ^~~~~~~~~~
main.cpp:19:9: error: ‘std::string {aka class std::basic_string}’ 
has no member named ‘push_front’; did you mean ‘push_back’?
     str.push_front('l');
         ^~~~~~~~~~
main.cpp:20:9: error: ‘std::string {aka class std::basic_string}’ 
has no member named ‘push_front’; did you mean ‘push_back’?
     str.push_front('p');
         ^~~~~~~~~~

Explanation:

It will generate compilation errors because the push_front() function is not available in the string class.

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++ Strings | Find output programs | Set 5... >>
<< C++ Strings | Find output programs | Set 3...