java - Trying to create a palindrome...how can I compare the "output.charAt(k)" to original "output" String? -
public static void main(string[] args) { string input = new string(joptionpane.showinputdialog("enter string.")); string inputlow = input.tolowercase(); string output = ""; //blank string for(int = 0; < inputlow.length(); i++) //for loop continue through entered string { if(inputlow.charat(i) >= 'a' && inputlow.charat(i) <= 'z') //if statement check appropriate characters { output += inputlow.charat(i); //add appropriate characters string output ('a' - 'z') } } system.out.println(output); //getting reverse string int n = output.length() - 1; string last = ""; for(int k = n; k >= 0; k--) { system.out.print(output.charat(k)); //last = string.valueof(output.charat(k)); } //system.out.println(""); //system.out.println(last); }
so trying print string last when un-comment code outputs this:
heyman namyeh h
but want print "heyman" on third line. (i doing print statements test if doing correctly, , goal compare string last string output , if same palindrome, otherwise it's not.)
how can method (or it)?
you're setting value of last 1 character @ time. you're resetting every time, why ends on last character of reversed string (which first character).
change last = string.valueof(output.charat(k)) + last;
Comments
Post a Comment