For Details of The Program: CLICK HERE
import java.io.*;
class Prog3
{
String str1,str2;
int len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeSentence() throws Exception
{
System.out.print("\nEnter the sentence:");
str1=br.readLine().trim();
len=str1.length();
char ch=str1.charAt(len-1);
if(ch!='.' && ch!='?' && ch!='!')
{
System.out.print("\nINVALID INPUT:");
return;
}
show();
}
private void show()
{
String s="",s1;
s1=str1.substring(0,len-1);
str2="";
while(true)
{
int i=s1.indexOf(' ');
if(i< 0)
break;
s=s1.substring(0,i);
s1=s1.substring(i+1);
str2=str2+" "+rev(s);
}
str2=str2+" "+ rev(s1);
System.out.print("\n"+str1);
System.out.print("\n"+str2);
}
private String rev(String s)
{
s=makePalin(s);
return s;
}
private String makePalin(String s)
{
String s2,s3;
int i;
int len1=s.length();
s2="";
for(i=len1-1;i>=0;i--)
s2=s2+s.charAt(i);
if(!s.equals(s2))
{
for(i=len1-1;i>=0;i--)
{
if(s.charAt(i)!=s.charAt(i-1))
break;
}
/* EXTRACTING THE PART OF THE WORD WHICH WILL BE REVERSED. IN THE ABOVE LOOP, THE WE HAVE SEARCHED FROM WHERE REPEATATION OF END CHARACTERS STARTS SO THAT THE WORD WHICH WILL BE REVERSED CAN ELIMINATES THE REPEATED CHARACTERS. E.G. XAZZZ WILL BECOME XAZZZAX AS WE HAVE TAKED XA TO REVERSE IT AND CONCATENATE WITH XAZZZ */
s3=s.substring(0,i+1);
len1=s3.length();
s2="";
for(i=len1-1;i>=0;i--)
s2=s2+s3.charAt(i);
//s=s+s3;
s2=s2.substring(1);
return s+s2;
}
else
return s;
}
public static void main(String args[]) throws Exception
{
Prog3 ob=new Prog3();
ob.takeSentence();
}
}
Back To 2019 Computer Practical Paper: CLICK HERE
import java.io.*;
class Prog3
{
String str1,str2;
int len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeSentence() throws Exception
{
System.out.print("\nEnter the sentence:");
str1=br.readLine().trim();
len=str1.length();
char ch=str1.charAt(len-1);
if(ch!='.' && ch!='?' && ch!='!')
{
System.out.print("\nINVALID INPUT:");
return;
}
show();
}
private void show()
{
String s="",s1;
s1=str1.substring(0,len-1);
str2="";
while(true)
{
int i=s1.indexOf(' ');
if(i< 0)
break;
s=s1.substring(0,i);
s1=s1.substring(i+1);
str2=str2+" "+rev(s);
}
str2=str2+" "+ rev(s1);
System.out.print("\n"+str1);
System.out.print("\n"+str2);
}
private String rev(String s)
{
s=makePalin(s);
return s;
}
private String makePalin(String s)
{
String s2,s3;
int i;
int len1=s.length();
s2="";
for(i=len1-1;i>=0;i--)
s2=s2+s.charAt(i);
if(!s.equals(s2))
{
for(i=len1-1;i>=0;i--)
{
if(s.charAt(i)!=s.charAt(i-1))
break;
}
/* EXTRACTING THE PART OF THE WORD WHICH WILL BE REVERSED. IN THE ABOVE LOOP, THE WE HAVE SEARCHED FROM WHERE REPEATATION OF END CHARACTERS STARTS SO THAT THE WORD WHICH WILL BE REVERSED CAN ELIMINATES THE REPEATED CHARACTERS. E.G. XAZZZ WILL BECOME XAZZZAX AS WE HAVE TAKED XA TO REVERSE IT AND CONCATENATE WITH XAZZZ */
s3=s.substring(0,i+1);
len1=s3.length();
s2="";
for(i=len1-1;i>=0;i--)
s2=s2+s3.charAt(i);
//s=s+s3;
s2=s2.substring(1);
return s+s2;
}
else
return s;
}
public static void main(String args[]) throws Exception
{
Prog3 ob=new Prog3();
ob.takeSentence();
}
}
Back To 2019 Computer Practical Paper: CLICK HERE
No comments:
Post a Comment