Computer Science
Write a program in Java to accept a four-letter word. Display all the probable four letter combinations such that no letter should be repeated in the output within each combination.
Sample Input:
PARK
Sample Output:
PAKR, PKAR, PRAK, APRK, ARPK, AKPR, and so on.
Answer
import java.util.Scanner;
public class KboatStringCombinations
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word:");
String str = in.nextLine();
int len = str.length();
if (len != 4) {
System.out.println("Invalid Input!");
System.out.println("Please enter a four letter word");
return;
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
for (int k = 0; k < len; k++) {
for (int l = 0; l < len; l++) {
if (i != j && i != k && i != l
&& j != k && j != l
&& k != l) {
System.out.print(str.charAt(i));
System.out.print(str.charAt(j));
System.out.print(str.charAt(k));
System.out.println(str.charAt(l));
}
}
}
}
}
}
}
Output
Related Questions
While typing, a typist has created two or more consecutive blank spaces between the words of a sentence. Write a program in Java to eliminate multiple blanks between the words by a single blank.
Sample Input:
Indian Cricket team tour to AustraliaSample Output:
Indian Cricket team tour to AustraliaWrite a program in Java to accept a string and display the new string after reversing the characters of each word.
Sample Input:
Understanding Computer ScienceSample output:
gnidnatsrednU retupmoC ecneicSWrite a program in Java to accept two strings. Display the new string by taking each character of the first string from left to right and of the second string from right to left. The letters should be taken alternatively from each string. Assume that the length of both the strings are same.
Sample Input:
String 1: HISTORY
String 2: SCIENCESample Output:
HEICSNTEOIRCYSA new advanced Operating System, incorporating the latest hi-tech features has been designed by Opera Computer Systems. The task of generating copy protection code to prevent software privacy has been entrusted to the Security Department. The Security Department has decided to have codes containing a jumbled combination of alternate uppercase letters of the alphabet starting from A up to K (namely among A, C, E, G, I, K). The code may or may not be in the consecutive series of alphabets. Each code should not exceed 6 characters and there should be no repetition of characters. If it exceeds 6 characters, display an appropriate error message.
Write a program to input a code and its length. At the first instance of an error display "Invalid" stating the appropriate reason. In case of no error, display the message "Valid".
Sample Data:
Input:
n=4
ABCEOutput:
Invalid! Only alternate letters permitted!Input:
n=4
AcIKOutput:
Invalid! Only uppercase letters permitted!Input:
n = 7Output:
Error! Length of String should not exceed 6 characters!Input:
n=3
ACEOutput:
ValidInput:
n=5
GEAIKOutput:
Valid