SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Decode Encrypted Code Program in Java and Python

19 October 2013

ISC 2003 encrypted code decoding program with algorithm, explanation, Java solution and simple Python solution.

Question:

A simple encryption system uses a shifting process to hide a message. The shift value can be from 1 to 26. Spaces in the original text are replaced with QQ before encryption. The coded message is printed in blocks of six characters. Write a program that accepts the coded text and shift value, then prints the decoded text. Reject invalid shift values.

INPUT: Enter Coded Text: UHINBYLKKQCHHYLKK Enter the Shift Value: 7 OUTPUT: Decoded Text: ANOTHER WINNER INPUT: Enter Coded Text: RUIJGGEVGGBKSAGG Enter the Shift Value: 11 OUTPUT: Decoded Text: BEST OF LUCK

Algorithm:

Step 1: Start.

Step 2: Accept the coded text and convert it to uppercase.

Step 3: If the coded text length is 100 or more, display invalid length and stop.

Step 4: Accept the shift value.

Step 5: If the shift is less than 1 or greater than 26, display invalid shift value and stop.

Step 6: Add one blank space at the end of the coded text to safely read the next character.

Step 7: Initialize decoded string dec as blank.

Step 8: Scan each character of the original coded length.

Step 9: Add shift - 1 to the character code to decode it.

Step 10: If two consecutive decoded characters form QQ, append a blank space and skip the second Q.

Step 11: If the decoded character crosses Z, subtract 26 to wrap around.

Step 12: Ignore block spaces and append valid decoded characters to dec.

Step 13: Display the decoded text.

Step 14: Stop.

Explanation:

The program decodes the encrypted text by shifting every coded character forward in the alphabet. The input string is converted to uppercase so that all character calculations are done within the range A to Z. The length is checked first because the question allows coded text only when its length is less than 100.

The shift value is then validated. Since the alphabet has 26 letters, the shift must be from 1 to 26. For each character, the program adds shift - 1 to its character value. If this value crosses Z, 26 is subtracted to bring it back into the alphabet range. This creates circular movement through the alphabet.

The program also reads the next character along with the current character because the decoded pair QQ has a special meaning: it represents a blank space. An extra blank is added at the end of the input string so that reading the next character remains safe even near the end. If the decoded current and next characters form QQ, one space is added to the answer and the loop skips the second character of the pair. Otherwise, the decoded character is appended to dec.

The program is not simply replacing characters blindly; it is applying a circular alphabet shift. This means that after Z, the calculation must return to A. The subtraction of 26 performs this wrap-around. The use of both ch1 and ch2 is necessary because a blank is not represented by a single decoded character, but by the pair QQ. When such a pair is found, the index is advanced extra so that the second character of the pair is not decoded again. This keeps the spacing of the original message correct.

Java Program:

Java
/**
* The class Decode_ISC2003 inputs an encrypted coded text and then decodes it by adding the given shift code
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2003, Question 1
*/

import java.util.Scanner;

class Decode_ISC2003
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Coded Text: ");
        String s = sc.nextLine().toUpperCase(); // Work in uppercase as required by the question.
        int l = s.length(); // Store length before adding the extra space later.

        // The coded text is valid only when its length is less than 100.
        if(l >= 100)
        {
            System.out.println("INVALID LENGTH OF CODED TEXT");
            return;
        }

        System.out.print("Enter the Shift Value: ");
        int shift = sc.nextInt();

        // Shift must be between 1 and 26 because there are 26 alphabets.
        if(shift < 1 || shift > 26)
        {
            System.out.println("INVALID SHIFT VALUE");
            return;
        }

        s = s + " "; // Extra space allows safe reading of the next character.
        String dec = "";

        for(int i = 0; i < l; i++)
        {
            char ch1 = s.charAt(i);
            char ch2 = s.charAt(i + 1);

            int a = ch1 + shift - 1; // Shift the current character forward.
            int b = ch2 + shift - 1; // Shift the next character to test the QQ pair.

            // If the shift crosses Z, wrap around to the beginning of the alphabet.
            if(a > 90)
            a = a - 26;
            if(b > 90)
            b = b - 26;

            // Decoded QQ represents a blank space in the original text.
            if((char)a == 'Q' && (char)b == 'Q')
            {
                dec = dec + " ";
                i++; // Skip the second Q because both characters formed one blank.
            }
            else if(ch1 != ' ')
            {
                dec = dec + (char)a; // Add the decoded character to the final text.
            }
        }

        System.out.println("Decoded Text: " + dec);
    }
}

Equivalent Python Program:

Python
s = input("Enter Coded Text: ").upper()

# The question allows coded text with length less than 100.
if len(s) >= 100:
    print("INVALID LENGTH OF CODED TEXT")
else:
    shift = int(input("Enter the Shift Value: "))

    # Shift must stay within the allowed alphabet range.
    if shift < 1 or shift > 26:
        print("INVALID SHIFT VALUE")
    else:
        s = s + " "
        dec = ""
        i = 0

        # Decode each character and handle QQ as one blank space.
        while i < len(s) - 1:
            ch1 = s[i]
            ch2 = s[i + 1]

            a = ord(ch1) + shift - 1
            b = ord(ch2) + shift - 1

            if a > ord('Z'):
                a = a - 26
            if b > ord('Z'):
                b = b - 26

            if chr(a) == 'Q' and chr(b) == 'Q':
                dec = dec + " "
                i = i + 2
            else:
                if ch1 != ' ':
                    dec = dec + chr(a)
                i = i + 1

        print("Decoded Text:", dec)

Output:

Enter Coded Text: RUIJGGEVGGBKSAGG Enter the Shift Value: 11 Decoded Text: BEST OF LUCK

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →