Skip to content
Advertisement

String anagram in Java

i am trying to create a program for string anagram that follows these conditions:

  1. method should allow only letters, white space, commas and dots in an anagram. If there are any other characters, then the string cannot contain an anagram.
  2. The method should ignore all white space, commas and dots when it checks the text.
  3. If there are no letters in the text, then the text cannot be an anagram.
JavaScript

However when the program tests these 2 sentences, they are not anagram but they should be anagram. I have no idea where to look for mistake.

  1. Eleven plus two is thirteen.
  2. Twelve plus one is thirteen.

Advertisement

Answer

If you start your method as follows, it will fulfil validations mentioned in the 1st and the 3rd points of your question:

JavaScript

The next thing you should do is to replace all white space, commas and dots with "" in order to ignore them:

JavaScript

The complete code is as follows:

JavaScript

A test run:

JavaScript

Another test run:

JavaScript

Another test run:

JavaScript

Another test run:

JavaScript

Another test run:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement