# Tricky C++ question

**URL:** <https://boards.straightdope.com/t/tricky-c-question/212929>\
**Category:** Factual Questions\
**Created:** [November 12, 2003, 11:44pm UTC](https://boards.straightdope.com/t/tricky-c-question/212929 "2003-11-12T23:44:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HPL](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@HPL](https://boards.straightdope.com/u/HPL)\
**Post date:** [November 12, 2003, 11:44pm UTC](https://boards.straightdope.com/t/tricky-c-question/212929/1 "2003-11-12T23:44:40Z")

</div>

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.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bup](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bup](https://boards.straightdope.com/u/bup)\
**Post date:** [November 13, 2003, 12:27am UTC](https://boards.straightdope.com/t/tricky-c-question/212929/2 "2003-11-13T00:27:33Z")

</div>

pLastThreeFirstLine = strchr(pString, ’  
') - 3;

pLastThreeSecondLine = strchr(&pLastThreeFirstLine[4], ’  
') -3;

etc.

More c-ish than C++, but it’d point at the last three chars of each line.

---

<div class="post-metadata">

**Author:** ![np\_complete](https://avatars.discourse-cdn.com/v4/letter/n/ecb155/32.png) [@np\_complete](https://boards.straightdope.com/u/np_complete)\
**Post date:** [November 13, 2003, 12:41am UTC](https://boards.straightdope.com/t/tricky-c-question/212929/3 "2003-11-13T00:41:11Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![HPL](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@HPL](https://boards.straightdope.com/u/HPL)\
**Post date:** [November 13, 2003, 12:55am UTC](https://boards.straightdope.com/t/tricky-c-question/212929/4 "2003-11-13T00:55:44Z")

</div>

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?

Believe me, I’d use strings…

---

<div class="post-metadata">

**Author:** ![np\_complete](https://avatars.discourse-cdn.com/v4/letter/n/ecb155/32.png) [@np\_complete](https://boards.straightdope.com/u/np_complete)\
**Post date:** [November 13, 2003, 1:12am UTC](https://boards.straightdope.com/t/tricky-c-question/212929/5 "2003-11-13T01:12:30Z")

</div>

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.
