Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class RunLengthCode {
  4.  
  5. String pText;
  6. String cText;
  7.  
  8. RunLengthCode() {
  9. pText = "";
  10. cText = "";
  11. }
  12. void setPText(String txt) {
  13. pText = txt;
  14. }
  15.  
  16. void setCText(String txt) {
  17. cText = txt;
  18. }
  19.  
  20. String getPText() {
  21. return pText;
  22. }
  23.  
  24. String getCText() {
  25. return cText;
  26. }
  27.  
  28. void compress() {
  29. String ch = "";
  30. ch += pText.charAt(0);
  31. int cnt = 1;
  32. for(int i = 0; i < pText.length()-1; i++) {
  33. if(pText.charAt(i) == pText.charAt(i)+1)
  34. cnt++;
  35. else {
  36. if(cnt > 2) {
  37. ch += ch.charAt(i+1);
  38. ch += cnt;
  39. }
  40. else {
  41. if(cnt == 2) {
  42. ch += pText.charAt(i);
  43. ch += pText.charAt(i);
  44. }
  45. else if (cnt == 1)
  46. ch += pText.charAt(i);
  47. }
  48. cnt = 1;
  49. }
  50. }
  51. ch += pText.charAt(pText.length()-1);
  52. ch += cnt;
  53. }
  54.  
  55. void decompress() {
  56.  
  57.  
  58. }
  59. }
  60. public class Hw9 {
  61.  
  62. public static void main(String[] args) {
  63. Scanner in = new Scanner(System.in);
  64. RunLengthCode myC = new RunLengthCode();
  65.  
  66. String pText, cText;
  67.  
  68. System.out.print("Enter a plain text consisting of only lower-case alphabets and spaces:");
  69. pText = in.nextLine();
  70. myC.setPText(pText);
  71. myC.compress();
  72. System.out.println(pText+" => "+myC.getCText());
  73.  
  74. System.out.print("Enter a compressed text consisting of only lower-case alphabets, spaces and digits:");
  75. cText = in.nextLine();
  76. myC.setCText(cText);
  77. myC.decompress();
  78. System.out.println(cText+" => "+myC.getPText());
  79.  
  80.  
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement