0% found this document useful (0 votes)
4 views2 pages

Java Program for Word Decoding

The Java program defines a class 'Decode' that decodes an encoded word based on character frequency. It accepts an encoded word, processes it to find valid characters according to their expected frequency, and displays both the encoded and decoded words. The main method orchestrates the flow by creating an instance of the class and invoking its methods for input, processing, and output.

Uploaded by

sagarg8587
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java Program for Word Decoding

The Java program defines a class 'Decode' that decodes an encoded word based on character frequency. It accepts an encoded word, processes it to find valid characters according to their expected frequency, and displays both the encoded and decoded words. The main method orchestrates the flow by creating an instance of the class and invoking its methods for input, processing, and output.

Uploaded by

sagarg8587
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

Scanner;

class Decode {
private String word; // to store the encoded word
private int length; // to store length of the encoded word
private String new_word; // to store the decoded word

// Default constructor
Decode() {
word = "";
new_word = "";
length = 0;
}

// Method to accept the encoded word


void acceptWord() {
Scanner sc = new Scanner([Link]);
[Link]("Enter the encoded word: ");
word = [Link]().toLowerCase(); // convert to lowercase
length = [Link]();
}

// Method to decode the word


void findWord() {
new_word = "";
int i = 0;

while (i < length) {


char ch = [Link](i);
int expectedCount = (ch - 'a') + 1; // a=1, b=2, c=3, ...

int count = 0;
int j = i;

// Count how many times current character repeats


while (j < length && [Link](j) == ch) {
count++;
j++;
}

// If count matches the expected frequency, it’s a valid letter


if (count == expectedCount) {
new_word += ch;
i = j; // Move to next group of letters
} else {
// Skip invalid group (in case of malformed input)
i++;
}
}
}

// Method to display both the encoded and decoded words


void Display() {
[Link]("Encoded Word : " + word);
[Link]("Decoded Word : " + new_word);
}

// Main function to run the program


public static void main(String[] args) {
Decode obj = new Decode(); // create object
[Link](); // input
[Link](); // process
[Link](); // output
}
}

You might also like