String comparisons? How?
by Josh Albrecht · in Torque Game Engine · 03/02/2002 (10:56 am) · 3 replies
I feel pretty stupid asking this question, but I just cant figure it out. :(
How can I tell if the last four characters of one string are equal to another string having a length of four characters?
Thanks. :)
How can I tell if the last four characters of one string are equal to another string having a length of four characters?
Thanks. :)
#2
03/02/2002 (12:19 pm)
Hey, thanks alot! I didnt think about getting the last four characters by using length-#. Its ugly, but it works, and thats what counts. :)
#3
Using a small character stack or a character queue will be much more efficient.
03/02/2002 (1:21 pm)
Ya, the algorithm length-# is too inefficient.Using a small character stack or a character queue will be much more efficient.
Torque Owner Matt Webster
String A - longer than or equal to 4 characters
String B - 4 characters
1. Get length (using strlen equivilant function used in Torque engine) of String A. This value will be referred to as length.
2. (length - 1) - 4 = targetLength // this shows how much you have to skip in String A to have 4 characters left over
3. get to character that is in element "targetLength"
4. from "targetLength" character to the null character, put that string into another place to store it (or overwrite the original string)
5. Do strcmp on the now truncated String A and the original String B.
Hopefully you understand :)
Another way is you could just invert the arrangement of characters ("cow" becomes "woc") and then compare that with strncmp().
It'd effectively allow you to look at the ends of the word as if they were the fronts. Easy huh?