Home »
C++ programming »
C++ find output programs
C++ Strings | Find output programs | Set 4
This section contains the C++ find output programs with their explanations on C++ Strings (set 4).
Submitted by Nidhi, on July 18, 2020
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;
}
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.
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;
}
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.
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;
}
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.