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;
}
Answer Program 1:
Output:
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:
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:
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