Saturday, March 01, 2008

Any other logic?

Yesterday a friend mine asked to help with her MS assignment, it is java so I got interested and solved it. But I was not satified with the logic which I provided, Is any other logic for this?

The aim is to print all possible 3 and 2 letter word from String "GOLD".

public class TestChars {
public static void main(String []args) {
String str = new String("abcdef");
char[] chara = str.toCharArray();
int count = 0;
int countForTwo = 0;
for(int i = 0; i < str.length(); i++) {
for(int j = 0 ; j < str.length(); j++){
if(i != j) {
System.out.println("" + chara[i] + chara[j]);
countForTwo++;
}
for(int k = 0; k < str.length(); k++) {
if( i != j && j != k && i != k) {
System.out.println("" + chara[i] + chara[j] + chara[k]);
count++;
}
}
}
}
System.out.println("" + count);
System.out.println("" + countForTwo);
}
}

BTW, Cricker match getting interested now, "Noxious Weed" really undesirable for Aussies.

No comments:

From Generative AI to Agentic AI: 2 Weeks of Building MyNewsAI.io

From Generative to Agentic AI: 2 Weeks Building MyNewsAI.io Over the last two weeks, I set out to evolve MyNewsAI...