KnowledgeBoat Logo

Computer Science

A 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
ABCE

Output:
Invalid! Only alternate letters permitted!

Input:
n=4
AcIK

Output:
Invalid! Only uppercase letters permitted!

Input:
n = 7

Output:
Error! Length of String should not exceed 6 characters!

Input:
n=3
ACE

Output:
Valid

Input:
n=5
GEAIK

Output:
Valid

Java

Java String Handling

4 Likes

Answer

import java.util.Scanner;

public class KboatCodeCheck
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter length: ");
        int len = in.nextInt();
        in.nextLine();
        
        if (len < 1 || len > 6) {
            System.out.println("Error! Length of String should not exceed 6 characters!");
            return;
        }
        
        System.out.println("Enter Code:");
        String code = in.nextLine();
        
        for (int i = 0; i < len; i++) {
            char ch = code.charAt(i);
            
            if (Character.isLowerCase(ch)) {
                System.out.println("Invalid! Only uppercase letters permitted!");
                return;
            }
            
            if (ch < 'A' || ch > 'K') {
                System.out.println("Invalid! Only letters between A and K are permitted!");
                return;
            }
            
            /*
             * ASCII Code should be odd as we
             * are taking alternate letters
             * between A and K. We have already
             * checked above that letter is
             * between A and K. Now if we check
             * for odd then it means that letter
             * is one of A, C, E, G, I, K
             */
            
            if (ch % 2 == 0) {
                System.out.println("Invalid! Only alternate letters permitted!");
                return;
            }
            
            /*
             * Check for repetition
             */
            
            int count = 0;
            for (int j = 0; j < len; j++) {
                if (ch == code.charAt(j)) {
                    count++;
                }
            }
            
            if (count > 1) {
                System.out.println("Invalid! Letter repetition is not permitted!");
                return;
            }
        }
        
        System.out.println("Valid");
    }
}

Output

BlueJ output of A 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 ABCE Output: Invalid! Only alternate letters permitted! Input: n=4 AcIK Output: Invalid! Only uppercase letters permitted! Input: n = 7 Output: Error! Length of String should not exceed 6 characters! Input: n=3 ACE Output: Valid Input: n=5 GEAIK Output: ValidBlueJ output of A 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 ABCE Output: Invalid! Only alternate letters permitted! Input: n=4 AcIK Output: Invalid! Only uppercase letters permitted! Input: n = 7 Output: Error! Length of String should not exceed 6 characters! Input: n=3 ACE Output: Valid Input: n=5 GEAIK Output: ValidBlueJ output of A 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 ABCE Output: Invalid! Only alternate letters permitted! Input: n=4 AcIK Output: Invalid! Only uppercase letters permitted! Input: n = 7 Output: Error! Length of String should not exceed 6 characters! Input: n=3 ACE Output: Valid Input: n=5 GEAIK Output: ValidBlueJ output of A 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 ABCE Output: Invalid! Only alternate letters permitted! Input: n=4 AcIK Output: Invalid! Only uppercase letters permitted! Input: n = 7 Output: Error! Length of String should not exceed 6 characters! Input: n=3 ACE Output: Valid Input: n=5 GEAIK Output: ValidBlueJ output of A 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 ABCE Output: Invalid! Only alternate letters permitted! Input: n=4 AcIK Output: Invalid! Only uppercase letters permitted! Input: n = 7 Output: Error! Length of String should not exceed 6 characters! Input: n=3 ACE Output: Valid Input: n=5 GEAIK Output: ValidBlueJ output of A 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 ABCE Output: Invalid! Only alternate letters permitted! Input: n=4 AcIK Output: Invalid! Only uppercase letters permitted! Input: n = 7 Output: Error! Length of String should not exceed 6 characters! Input: n=3 ACE Output: Valid Input: n=5 GEAIK Output: Valid

Answered By

3 Likes


Related Questions