Our next problem is another puzzle, but I suggest we take a different approach as we solve it. Learning theorists describe and use many different instructional techniques. However, at their most fundamental level, they all share the goal of helping learners progress efficiently from novices to experts within a specified domain. Knowing how to solve problems within that domain is a significant skill developed through extended practice, separating experts and novices. Two previous examples, cpalnumber.cpp and palnumber.cpp, demonstrate the basic problem solution and some of the needed conversion operations. Another crucial skill is learning how to read and use language-specific documentation, and I'll provide hints and links to help you get started, narrowing your search. Try to solve the problems before studying the solutions below.
The cube and palindrome-number problems have three common steps:
See the Searching section for a list of C++ string class and C-string searching functions. The top table row labels the string representation for each column. At the top of the page, there are also links to more detailed function descriptions.
Problem 1: solve the problem using the C++ string class and its functions. You can follow the palnumber example, testing the first and last characters by indexing into the string. To expand your experience, I suggest using the find functions (see string Class Functions, especially Figure 4).
Problem 2: solve the problem using C-strings. Although there are C-string functions we can use to examine the first and last characters, using them as needed by the problem is challenging. I recommend following the palnumber example, testing them by indexing the string. Use the C-string strstr function to test for the presence of the "000" substring. Once you have the indexing version working, try replacing the indexing with the strchr and strrchr functions. Please see More C-String Functions, especially Figures 1 and 2, for details and examples of the three C-string functions.
There are many ways to program solutions for these relatively simple problems. We could follow the case-analysis pattern, based on the break and continue operators, illustrated in the palindrome-number programs. Instead, the following programs use an if-statement with a chain of logical-AND operations. The programs organize the sub-expressions in the if-statement to achieve the same benefits as the case analysis. Recall that: