0

Okay, so I'm coding a program that will tell the user if the word they put into the command line is an isogram or not, and I'm having difficulties.

How exactly do I go about making it to where the user inputted char array is compared against the other to see if it has more than one letter than the other?

I have this and I'm confused where to go from here.

char[] alphabet = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] wordLetters = word.toCharArray(); 
boolean blnResult = Arrays.equals(alphabet,wordLetters);

I'm sure I went in the completely wrong way with this, but how should I go about comparing the two to get that result?

Wes
  • 3
  • 3
  • what is wrong with it? – Scary Wombat Mar 02 '16 at 02:43
  • It runs fine, but it's not doing what I want. It's saying any word I input on the command line is an isogram. For instance, say I input salad, that isn't an isogram but it says it is. – Wes Mar 02 '16 at 02:46
  • see https://web.stanford.edu/class/archive/cs/cs106a/cs106a.1152/handouts/50-additional-practice-solutions.pdf – Scary Wombat Mar 02 '16 at 02:49

2 Answers2

2

if I understand your problem right this is how I would do it:

String isogram = "salad";
boolean isIsogram = !isogram.matches( ".*(.).*\\1.*" );
System.out.println( isIsogram );

or inefficient but if you have to use arrays:

String isogram = "salad";
boolean isIsogram = true;
for ( int i = 0; i < isogram.length(); i++ ) {
    for ( int j = 0; j < isogram.length(); j++ ) {
        if ( i != j && isogram.charAt( i ) == isogram.charAt( j ) && ( Character.isLetter( isogram.charAt( i ) ) ) ) {
            isIsogram = false;
        }
    }
}
System.out.println( isIsogram );
Zaid Qureshi
  • 1,203
  • 1
  • 10
  • 15
0

Basically isograms are word or phrase without a repeating letter.

Your function signature or program output expectation in the form of code would help answer better.

However, considering you're working on a function which returns true or false: Set is the way to go about it. Keep putting the characters in a Set or HashMap and if while inserting in set, the character already exists in set then return the response as false otherwise return true.

Milind Gokhale
  • 575
  • 2
  • 14