I need have a tricky little task and I’m not sure how to accomplish it.
My program prompts people to enter 4 lines of text. Then I’m supposed to(among other things), read in the last 3 and compare them.
Example: the last three characters in the first line must be the same as those as the last three characters in the third line. Ditto with second and fourth lines. The newline character is not counted. I assume this has something to do with counting back from the newline to net the last three but I’m not quite sure how to do that.
There are lots of ways to accomplish this particular goal, but they largely depend on how you’re fetching your lines from the console and how you’re storing the data.
I would suggest using std::getline() (found in <string>) to fetch the lines and std::string (also in <string>) to represent the lines. std::getline() will discard the ’
’ (if it found one) so you don’t need to worry about that and you can perform comparisons using the well-known stl iterator idioms. Since you’re dealing with the last 3 characters, a std::string::reverse_iterator should work nicely (std::string.rbegin() and std::string.rend() provide reverse iterators).
If you’re using other types of storage and string-fetching, then there are techniques you can use with those, though they’re likely to be messier (i.e. if you use char to hold your lines, you’ll need to use std::strlen() (found in <cstring>) and perform some index juggling to perform this task).
I know this is going to sound masochistic, but I don’t have a lot of choice. What if strings are out of the question and the lines must be read in character by character?
In that case I’d parse the strings into 4 character arrays while reading them in (advance to a new char array whenever you read a ’
') and make extensive use of std::strlen(). Make sure you terminate your char arrays properly (otherwise std::strlen() won’t work right). If std::strlen() of every string is at least 3, then the offset of the third-from-last character will be std::strlen() of the string in question - 3, a pointer to that char would be str_start + offset_of_last_3, and in this special case you can compare them using std::strcmp() (you can use std::strcmp() only becasue of the ‘\0’ that follows the final 3. If you needed to compare other substrings, you could use a manual loop or std::strncmp().
I assume this is for a class project, so I won’t post any code, but out in the real world you really should use std::string or std::vector<char> whenever it’s possible (it’s also wise to use the standard algorithms whenever they do the right thing).
If your instructor isn’t permitting you to use the library then your instructor isn’t teaching you C++, ip’s teaching you C (though there are a couple of good reasons, i.e. in a data structures course you’d want the students to implement their own linked-lists instead of using std::list). I would recommend doing the assignments first using the library (for your own benefit) and then implenting them within your instructor’s rules.