Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:53:29+00:00 2026-05-26T22:53:29+00:00

public class Anagram { public static void main(String[] args) { String a = Despera

  • 0
public class Anagram {
    public static void main(String[] args) {

        String a = "Despera tion-".toLowerCase();
        String b = "A Rope Ends It".toLowerCase();

        String aSorted = sortStringAlphabetically(a);
        String bSorted = sortStringAlphabetically(b);

        if(aSorted.equals(bSorted)){
            System.out.println("Anagram Found!");
        }else{
            System.out.println("No anagram was found");
        }

    }

    public static String sortStringAlphabetically(String s) {

        char[] ca = s.toCharArray();
        int cnt = 0;
        ArrayList al = new ArrayList();

        for (int i = 0; i < ca.length; i++) {
            if (Character.isLetter(ca[cnt])) 
                al.add(ca[cnt]);

            cnt++;
        }

        Collections.sort(al);
        return al.toString();
    }
}

As a learner, I hacked up this boolean Anagram checker. My chosen solution was to create a sortStringAlphabetically method seems to do just too much type-juggling String -> chars[] -> ArrayList ->String – given that I do just want to compare 2 strings to test whether one phrase is an anagram of another – could I have done it with less type-juggling?

ps The tutors solution was a mile away from my attempt, and probably much better for a lot of reasons – but I am really trying to get a handle on all the different Collection types.

http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/doc/src-html/de/hska/java/exercises/arrays/Anagram.html#line.18

EDIT

FTW here is the original challenge, I realise I wandered away from the solution.

http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/arrays.html

My initial kneejerk reaction was to simply work though array a, knocking out those chars which matched with array b – but that seemingly required me to rebuild the array at every iteration – Many thanks for all your efforts to educate me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:53:30+00:00Added an answer on May 26, 2026 at 10:53 pm

    There are different ways to improve this, if you go with this algorithm.
    First, you don’t necessarily need to create a character array. You can use String.charAt() to access a specific character of your string.

    Second, you don’t need a list. If you used a SortedMultiSet or a SortedBag, you could just add things in sorted order. If you write a function that creates the SortedMultiSet from your string, you could just compare the sets without rebuilding the string.

    Note: I don’t know what libraries you’re allowed to use (Google and Apache have these types), but you can always ‘brew your own’.

    Also, make sure to use generics for your types. Just defining ArrayLists is pretty risky, IMHO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE;
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class Test { public static void main(String[] args) throws Exception { String s1
public class Main { public static void main (String args[]) { int nums[]= {2,
public class Main { public static void main(String[] args) throws InterruptedException { ClassA a
public class Guess { public static void main(String[] args){ <sometype> x = <somevalue>; System.out.println(x

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.