0
class decoding
{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the code");
String c=in.readLine();
int i,j,l,d,n[],k,p=0;
String a,b="",z="";
l=c.length();
n=new int[l];
char h;
for(i=l-1;i>=0;i--)
{
b=b+c.charAt(i);
}
for(j=0;j<l;j++)
{
d=(int)b.charAt(j);
d-=48;
n[j]=d;
}
for(k=0;k<l-2;k++)
{
if(n[k]<10)
{
p=n[k]*10+n[k+1];
if(p<65&&p>90&&p<97&&p>122)
{
if(p<48&&p>57&&p!=32)
{
p=n[k]*100+n[k+1]*10+n[k+2];
}
}
if(p>=65&&p<=90&&p>=97&&p<=122||p==32)
{
h=(char)p;
z=z+h;
}
}
}
System.out.println(z);
}
}
4 answers
0
Think about this condition for a moment:
if(p<65&&p>90&&p<97&&p>122) ...
Will this ever be true?
Reframe your logic and then post the code here.
0
if(p<65||p>90||p<97||p>122)
if the next ascii code is 118,
n[k]=1,n[k]*10 + n[k+1] = 11
the above condition will be true, and then 1*100+1*10+8 = 118. :O
0
Still incorrect! For two digit codes: if((p>=65 && p<=90) || (p>=97 && p<=99)) and for three digit codes: if(p>=100 && p<=122). Try this.
1
This is the final corrected code:
import java.io.*;
class decoding
{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the code");
String c=in.readLine();
int i,j,l,d,n[],k,p=0;
String a,b="",z="";
l=c.length();
n=new int[l];
char h;
for(i=l-1;i>=0;i--)
{
b=b+c.charAt(i);
}
for(j=0;j<l;j++)
{
d=(int)b.charAt(j);
d-=48;
n[j]=d;
}
for(k=0;k<l-1;k++)
{
// if(n[k]<10)
// {
p=n[k]*10+n[k+1];
if((p>=65 && p<=90) || (p>=97 && p<=99) || p == 32)
{
p=n[k]*10+n[k+1];
k++;
}
else
{
p=n[k]*100 + n[k+1]*10 + n[k+2];
k=k+2;
}
z = z +(char)p;
// }
}
System.out.println(z);
}
}
/*
For Input: 2312179862310199501872379231018117927
it will print: Have a Nice Day
*/
Log in to your account to answer this questions.