Strings help (new user)
in
Programming Questions
•
1 year ago
Hey all,
I was trying to do something using strings but i've hit a major stone wall..
What i need to is create a few methods and strings which
inputs a string from the user, and displays “Mirror image!” if the String is a mirror image
, and “Meh, another boring image :(” otherwise.
For example "madam" is not considered a mirror image because of the "d" however it IS a palindrome.
A mirror image would be a word such as "HOIOH" & "lol"
There is also a list of letters which, if mirrored would be considered true in this particular case... these are:
ʻ
Aʼ, ʻHʼ, ʻIʼ, ʻMʼ, ʻOʼ, ʻTʼ, ʻUʼ, ʻVʼ, ʻWʼ, ʻXʼ, ʻYʼ, ʻiʼ, ʻmʼ, ʻnʼ, ʻoʼ, ʻuʼ, ʻvʼ, ʻwʼ, ʻxʼ
(
ʻ
m
ʼ
,
ʻ
n
ʼ
,
ʻ
u
ʼ
are SLIGHTLY different in the mirror, but in an acceptable range)
So far i have the following code...
import javax.swing.JOptionPane;
void setup() {
size(400, 300);
background(255);
textSize(24);
textAlign(CENTER);
fill(0);
size(400, 300);
background(255);
textSize(24);
textAlign(CENTER);
fill(0);
String input = JOptionPane.showInputDialog("Enter your word");
if (IsMirrorImage(input)==true)
{
text ("Mirror image!", width/2, height/2);
}
else
{
text("Meh, another boring image :(", width/2, height/2);
}
}
I have created 2 methods to aid in my design and they are also below..
//reversing the string entered
String reverse(String input) {
String result = "";
for(int i=0; i<input.length(); i++)
result = input.charAt(i)+result;
return result;
}
String reverse(String input) {
String result = "";
for(int i=0; i<input.length(); i++)
result = input.charAt(i)+result;
return result;
}
//checking if its a palindrome using the above method
boolean isPalindrome(String input) {
if(input.equals(reverse(input)))
return true;
else
return false;
}
boolean isPalindrome(String input) {
if(input.equals(reverse(input)))
return true;
else
return false;
}
//------------------------------------------------------------------------------------------
String test = "AHIMOTUVWXY"+"imnouvwx";
boolean IsMirrorImage(String input){
for (int i=0; i<input.length(); i++)
if(indexOf(input, i)==(String test))
{
return true;
}
else
{
return false;
}
I know i have severely butchered the IsMirrorImage method so if anyone could give me some advice instead of saying "Incorrect code! Heres the right code!" it would be greatly appreciated.
1