Decode ASCII Code Program in Java | ISC Computer Science 2023 Paper 2

An encoded text can be decoded by finding actual character for the given ASCII code in the encoded message. Write a program to input an encoded text having only sequence of ASCII values without any spaces. Any code or value which is not in the range 65 – 90 (A – Z) or 97 – 122 (a – z) or 32 (for space) will be ignored and should not appear in the output message.

Write a program to accept a code which contains only digits (0 to 9). Display an error message if the code contains any other character(s).

Perform the following tasks:
(a) Decode the encoded text as per the above instructions
(b) The first alphabet of each word must be changed to uppercase and the remaining alphabets to lowercase
(c) Any consecutive sets of code 32 will be taken as only one blank space.
(d) Display it in the form of a sentence.

Test your program for the following data and some random data:

Example 1
INPUT: CODE: 10665771011153266797868
OUTPUT: James Bond

Example 2
INPUT: CODE: 667685693232837589
OUTPUT: Blue Sky

Example 3
INPUT: CODE: 22-768#5693232375889
OUTPUT: INVALID INPUT

import java.util.Scanner;
class Decode{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("CODE: ");
        String code = in.nextLine();
        if(!valid(code)){
            System.out.println("INVALID INPUT");
            return;
        }
        String msg = "";
        int i = 0;
        while(i < code.length()){
            int num = Integer.parseInt(String.valueOf(code.charAt(i)));
            while(num <= 122){
                if(num >= 'A' && num <= 'Z'){
                    if(msg.length() == 0 || msg.endsWith(" "))
                        msg += Character.toUpperCase((char)num);
                    else
                        msg += Character.toLowerCase((char)num);
                    break;
                }
                else if(num >= 'a' && num <= 'z'){
                    if(msg.length() == 0 || msg.endsWith(" "))
                        msg += Character.toUpperCase((char)num);
                    else
                        msg += Character.toLowerCase((char)num);
                    break;
                }
                else if(num == ' '){
                    if(!msg.endsWith(" ")){
                        msg += (char)num;
                        break;
                    }
                    else
                        break;
                }
                else if(i + 1 < code.length()){
                    num = Integer.parseInt(num + "" + code.charAt(i + 1));
                    i++;
                }
                else
                    i++;
            }
            i++;
        }
        System.out.println(msg);
    }
    public static boolean valid(String s){
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(!Character.isDigit(ch))
                return false;
        }
        return true;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *