There are a couple of things I would change:
1. Change the getText function to include the size of the C string you pass to it.
void getText(char cstr[], int size);
This function is the only time you have no means to determine the length of the string, it hasn't been entered yet. By including the maximum allowed size of the C string lets you choose a different size as wanted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
void getText(const char[], int size);
int my_strlen(const char[]);
void forward_print(const char[]);
void reverse_print(const char[]);
int main()
{
const int size { 256 };
char cstr[size];
getText(cstr, size);
forward_print(cstr);
reverse_print(cstr);
// print_backwards(cstr); // <--- this function no longer exists, remove
}
void getText(char cstr[], int size)
{
std::cin.getline(cstr, size);
}
| |
You choose later to have a C string length of 80 (79 characters + '\0') you need to change only one line of code. Line 8 in main. You should never use "magic numbers."
https://www.jarchitect.com/QACenter/index.php?qa=155&qa_1=no-magic-numbers
2. Why are you getting the C string's length in
main? You are doing nothing with it.
If you were to print out a message of the string's length, then fine.
std::cout << "The C string has a length of " << len << '\n';
3. You are including headers for library features you are not using. Not that it will affect the program, it is just a bit of overkill. A "toss in the kitchen sink" kind of thing.
It might take the preprocessor a couple of CPU cycles to slog its way through header code it doesn't need to.
With the code you currently have all you
need is
<iostream>.
4.
using namespace std;
. Don't. Period.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
I don't use it, I fully qualify each object from the std namespace with
std::
Personal pet peeve of mine, I refuse to use it. I consider it being lazy.
Others will disagree. YMMV.
5. Variable names. I was very particular in my name choices in the forward and reverse display functions. I was using the names of C++ container
iterators.
Look at the names of the predefined iterators for
std::string.
https://en.cppreference.com/w/cpp/string/basic_string
Look kinda familiar, don't they.
Having variable names like those is a good hint to your instructor you likely didn't write the code yourself.
6. Now, other people have given you decent code for the forward print function that does not use the iterator style for the for loop.
I wrote both the print functions deliberately to emulate iterator usage. Easier IMO to covert C string functions using iterator emulation to functions that take a
std::string that CAN use iterators:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
void forward_print(const std::string& str)
{
for (std::string::const_iterator itr = str.cbegin(); itr != str.cend(); itr++)
{
std::cout << *itr << ' ';
}
std::cout << '\n';
}
void reverse_print(const std::string& str)
{
for (auto itr = str.crbegin(); itr != str.crend(); itr++)
{
std::cout << *itr << ' ';
}
std::cout << '\n';
}
| |
Now, onto the commentary section......
7. ROFL. Not one bit, really. I simply tried to follow the instructions you were given, C strings and pointers, and purposefully wrote code in a way that gave a bit of insight into C++ containers and iterators.
Regular array pointers and C++ container iterators are superficially much the same. They act much the same, pointing to elements of the container and can be manipulated using rudimentary pointer math.
I routinely make more than my share of dumb "what was I thinking?!?" mistakes.
There are other little nitpicky details I might tell you about (not using
return 0;
or
system("pause);
) but I've bored you enough already.