I am having trouble figuring this problem out. I am trying to find the common suffix for two Strings and print it out. For example, two strings values such as "caption","action", I want to have their common suffix return "tion". For some reason, even though there are common suffix value, the result keeps throwing null.
Can you someone please tell me what is wrong with my code?
Also let me know if there is an easier and simpler way to get the same job done.
Thanks in advance!
well let's see, where is the problem? is it in commonSuffix or reverse?
Hint - never write more than a few lines of code (1 or 2) with out testing.
So put some print statements to find out where the issue lies. should be simple enough and the problem should become obvious rather easily.
like are you really comparing the last char in s1 with the last char in s2?
And yes i can think of a few ways that are simpler, like......
(and this is a bad, but simpler option)
in a loop - compare two substrings that are the last char of each, if they are equal
compare two substrings that are of size 2, then three ......
and of course look at the api to see if there are classes / methods to help.
Steve Fahlbusch wrote:
The reason why i created reverse method was to print the letters in the right order. My commonSuffix method compares their suffix from right to left, so if i just print that out, it will print backwards.
That is obvious, but why??? Why do what is provided by the language?
ahhh ic. I am learning from the text, "Introduction to java programming" and the text wants me to come up with that on my own. I haven't learn the reverse method stored in java yet.
so yea, I just learned something new! haha
sorry for my ignorance...
Albert Park wrote:so yea, I just learned something new!
Here's something else then: the check 'i > -1' may be slower than 'i >= 0', because the compiler (or CPU) may well be able to optimize the latter. I understand why you did it, but very often the simplest (and, in this case, the most readable) solution is the best.
Another one to think about: What about having your commonSuffix() method simply return the length of the common suffix? What you have certainly isn't wrong, but you might find that approach a bit more flexible.
Ashwin Sridhar wrote:And one more question , what if you have following input,
S1 = "abcdef";
S2 = "agchei";
Your code would return matching values at positions 0,2,4 and you would have abc as output but its not the suffix.
It won't.. See the else part.. It breaks as the first mismatch is found from the last..
Winston Gutkowski wrote:Here's something else then: the check 'i > -1' may be slower than 'i >= 0', because the compiler (or CPU) may well be able to optimize the latter.
To be fair, the compiler may well be able to optimize either one, given that for ints they are equivalent. It's difficult to anticipate exactly what optimizations end up getting put into code nowadays, and probably best not to say too much about it unless we're actively timing the results for a specific bit of code.
Winston Gutkowski wrote:I understand why you did it, but very often the simplest (and, in this case, the most readable) solution is the best.