Question

don't use system.exit() please

don't use system.exit() please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code for above problem

class Main
{
   public static void main(String args[])
   {
       String str="qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT";
       System.out.println("Original string: "+str);
       String encoded_str=encode(str);
       System.out.println("Encoded string: "+encoded_str);
       String decoded_str=decode(encoded_str);
       System.out.println("Decoded string: "+decoded_str);
   }
   public static String decode(String str)
   {
       if(str.length()==0)
           return "";
       char ch=str.charAt(0);
       if(Character.isDigit(ch))
       {
           int count=ch-'0';
           String temp=get_str(str.charAt(1),count);
           return temp+decode(str.substring(2));
       }
       return ch+decode(str.substring(1));
   }
   public static String get_str(char ch,int count)
   {
       if(count==0)
           return "";
       return ch+get_str(ch,count-1);
   }
  
  
   public static String encode(String str)
   {
       String encoded_str="";
       int len=str.length();
       int i=0;
       while(i<len)
       {
           int j=i+1;
           while(j<len && str.charAt(j)==str.charAt(i))
               j++;
           int count=j-i;
           if(count>1)
               encoded_str+=String.valueOf(count);
           encoded_str+=str.charAt(i);
           i=j;
       }
       return encoded_str;
   }
}

Sample output

Original string: WWWWWWWWWWeeeeerrtyyyyyqqqqwEETIT Encoded string: q9w5e2rt5y4qw2Er3T Decoded string: WWWWWWWWWWeeeeerrtyyyyy

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
don't use system.exit() please
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT