Ask coding questions

← Back to all posts
help needed urgently project work
nayomi21 (1)

an interactive java code asking a client to input a string and to test if the string is a palindrone

Commentshotnewtop
ArchieMaclean (584)

You would want to loop through the string and check that the first character is the same as the last, the second the same as the second last, and so on. I won't give you the whole solution but this should help you work it out:

You want to have a variable storing the length of the string (e.g. string_length). Then you want to loop string_length times, with a counter (say i). Then you can check if the first/last e.t.c. characters are equal using (assuming the string is called myString):
if (myString.charAt(i) == myString.charAt(string_length-i-1) // if it is equal
That -1 at the end is needed because of zero-indexing; without it, there would be an out of bounds exception.
If this is false, then you know that the string is not a palindrome.

I hope this helped, and please mark it as the answer if it solved your problem :) If you need more elaboration on any points, just reply to this comment and I will get back to you.

AdobeZev (8)

I dont know java but I think I could help in explaining it.
When you get the prompt and the user answers the question you should take the first 3 chars and the last 3 chars. Reverse the last 3 chars and check if both of the words equal the same. (make sure you make them both lowercase) and set a bool as true

mwilki7 (184)

@AdobeZev
To add on to this answer, if you want an easy way to reverse a string in Java:

String mystr = "Hello";
String reversed = (new StringBuilder(mystr)).reverse().toString();

must have import java.lang.StringBuilder; imported first though.