Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I am working on a Java program for Anagrams. I have a few issues i need to

ID: 3758038 • Letter: H

Question

Hello I am working on a Java program for Anagrams. I have a few issues i need to fix. These are the issues:

1. I need to better format the program.

2. You are missing a newline at the end of the output

3. Anagrams such as Otto and toot are not being recongized as Anagrams

Please help!!! Thanks!

Program so far:

package anagram;

import java.util.*;

public class Anagram {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter the first word : ");

String st = in.nextLine();

System.out.println("Enter the second word : ");

String st2=in.nextLine();

int a = st.length();

int b = st2.length();

char ch[] = new char[a];

char ch2[] = new char[b];

int e = 0;

for(int i = 0;i<a;i++)

{

char c = st.charAt(i);

ch[i]=c;

}

for(int i = 0;i<b;i++)

{

char c = st2.charAt(i);

ch2[i]=c;

}

System.out.println("Elements of ch2 are :");

for(int j=0;j<a;j++)

{

char ch3 = ch[j];

for(int k=0;k<b;k++)

{

char ch4 = ch2[k];

if(ch3==ch4)

e++;

}

}

if(e==a)

System.out.print(st2+" is an anagram of "+st);

else

System.out.print(st2+" is not a anagram of "+st);

}

}

Explanation / Answer

package anagram;

public class Anagram {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the first word : ");
String st = in.nextLine();
System.out.println("Enter the second word : ");
String st2=in.nextLine();
int a = st.length();
int b = st2.length();
int sum1=0;
int sum2=0;
for(int i = 0;i<a;i++)
{
sum1=sum1+st.charAt(i);
}
for(int i = 0;i<b;i++)
{
sum2=sum2+st2.charAt(i);
}
if(sum1==sum2)
System.out.print(st2+" is an anagram of "+st);
else
System.out.print(st2+" is not a anagram of "+st);
}
}

I have Modified Your Program

Simple operation: I have calculated the sum of characters of str1 and str2, if both sums are same they are anagrams.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote