Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class RunLengthCode {
- String pText;
- String cText;
- RunLengthCode() {
- pText = "";
- cText = "";
- }
- void setPText(String txt) {
- pText = txt;
- }
- void setCText(String txt) {
- cText = txt;
- }
- String getPText() {
- return pText;
- }
- String getCText() {
- return cText;
- }
- void compress() {
- String ch = "";
- ch += pText.charAt(0);
- int cnt = 1;
- for(int i = 0; i < pText.length()-1; i++) {
- if(pText.charAt(i) == pText.charAt(i)+1)
- cnt++;
- else {
- if(cnt > 2) {
- ch += ch.charAt(i+1);
- ch += cnt;
- }
- else {
- if(cnt == 2) {
- ch += pText.charAt(i);
- ch += pText.charAt(i);
- }
- else if (cnt == 1)
- ch += pText.charAt(i);
- }
- cnt = 1;
- }
- }
- ch += pText.charAt(pText.length()-1);
- ch += cnt;
- }
- void decompress() {
- }
- }
- public class Hw9 {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- RunLengthCode myC = new RunLengthCode();
- String pText, cText;
- System.out.print("Enter a plain text consisting of only lower-case alphabets and spaces:");
- pText = in.nextLine();
- myC.setPText(pText);
- myC.compress();
- System.out.println(pText+" => "+myC.getCText());
- System.out.print("Enter a compressed text consisting of only lower-case alphabets, spaces and digits:");
- cText = in.nextLine();
- myC.setCText(cText);
- myC.decompress();
- System.out.println(cText+" => "+myC.getPText());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement