gudii9
asked on
mirrorEnds challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p139411
I am not clear on challenge desciption and how to approach. please advise
I am working on below challenge
http://codingbat.com/prob/p139411
I am not clear on challenge desciption and how to approach. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What are they saying is :
when you inverse a string a (set of characters you ) what are the Characters that are the same in the beginning in both of the inverse string and the string it self
what you are going to do in your code is :
You have your string as input lets name it STRING
generate the inverse string lets name it INVERSE
start comparing the characters in both of the STRING and INVERSE starting from the character index 0
if you find that the index 0 character is the same you move on to the next Character index 1 and compare if they are the same you move on if they aren't you stop and return as result the only Characters that you found them match
some times you may find 0 character ,ex: acbx
some times you find the whole word ex : aba
I hope this help,
not 100% clear. still thinking
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mirrorEnds("abca") → "a"
mirrorEnds("aba") → "aba"
how first one is a and second ne is aba (i though second one also should return a??)
Have you tried doing some of these by hand with pencil and paper?yes tryinh
How many character compares are there in a 10-char string?10
How many in a 7-char string?7
What is the difference between even and odd string lengths?number of character in even string are even number like 8 , 12 etc where as base string are base number like 5 , 9
How are you going to handle the middle character? Compare it to itself, or not compare it at all?depends on before and after characters
ASKER
public String mirrorEnds(String string) {
String result = "";
int len = string.length();
for (int i = 0, j = len - 1; i < len; i++, j--)
if (string.charAt(i) == string.charAt(j))
result += string.charAt(i);
else break;
return result;
}
above passed all tests
public class MirrorEnds {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("value is-->"+mirrorEnds("aba"));
System.out.println("value is-->"+mirrorEnds("abca"));
}
public static String mirrorEnds(String string) {
String result = "";
int len = string.length();
for (int i = 0, j = len - 1; i < len; i++, j--)
if (string.charAt(i) == string.charAt(j))
result += string.charAt(i);
else break;
return result;
}
}
value is-->a
At least come up with some code you wrote yourself.
Starting from that we can (try to) help you further.