In C What string will the following code output string str
In C++
What string will the following code output?
string str = “crockpot of ontological orthodoxy”;
string output = “”;
int counter = 1;
while (str.at(counter) != ‘o’)
{
output += str.at(counter);
counter += 3;
}
cout << output << endl;
Solution
One can compile the following program using only the main function with the code given (as shown below) written in a file with the name generateString.cpp
//file name: generateString.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = \"crockpot of ontological orthodoxy\";
string output = \"\";
int counter = 1;
while ( str.at(counter) != \'o\')
{
output += str.at(counter);
counter += 3;
}
cout << output << endl;
return 0;
}
This program prints the following string on the terminal:
rktfnlilr
The logic is using the given string of \"crockpot of ontological orthodoxy\", starting from 2nd character (ie., from r, then k, then t in crockpot, then f in of etc) print the 3rd character till it becomes \'o\'. This considers blank space also as a character. Thus the output string is rktfnlilr.
On a Unix or linux platform compile the above program using the command g++ generateString.cpp. Then run the program as ./a.out to generate the above string.

