PROGRAM: Requested by Sir Ankur Acharya
ARRANGING WORDS IN A STRING ACCORDING TO THEIR POTENTIAL
The encryption of alphabets are to be done as follows:
A = 1
B = 2
C = 3
.
.
.
Z = 26
The potential of a word is found by adding the encrypted value of the alphabets.
Example: KITE
Potential = 11 + 9 + 20 + 5 = 45
Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order.
Output the result in format given below:
Example 1
INPUT : THE SKY IS THE LIMIT.
POTENTIAL : THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
OUTPUT : IS THE THE SKY LIMIT
Example 2
INPUT : LOOK BEFORE YOU LEAP.
POTENTIAL : LOOK = 53
BEFORE = 51
YOU = 61
LEAP = 34
OUTPUT : LEAP BEFORE LOOK YOU
SYNTAX
import java.io.*;
class Potential {
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A SENTENCE");
String d=br.readLine();
d=d.toUpperCase();
if(d.charAt(d.length()-1)=='!'||d.charAt(d.length()-1)=='.'||d.charAt(d.length()-1)=='?')
{
String s=d.substring(0,d.length()-1);
int temp=0;String ump="";String op="";
String a[]=s.split(" ");int q=64;int p[]=new int[a.length];
System.out.println("POTENTIALS ARE:");
for(int i=0;i<a.length;i++)
{
int sum=0;
String x=a[i];
for(int j=0;j<x.length();j++)
{
char ch=x.charAt(j);
int z=(int) ch-q;
sum+=z;
}
System.out.println(x+" "+sum);
p[i]=sum;
}
for (int i=0; i<a.length;i++)
{
for (int j=i+1;j<a.length;j++)
{
if(p[i]>p[j])
{
temp = p[i];
ump = a[i];
p[i] = p[j];
a[i]=a[j];
p[j] = temp;
a[j]=ump;
}
}
}
System.out.println("ARRANGED SENTENCE IS");
for(int i=0;i<a.length;i++)
{
op+=a[i]+" ";
}
System.out.print(op.trim()+".");
}
else
System.out.println("String must end with a Punctuation of ! . or ?");
}
}
Thanks a lot Sir.
ReplyDeletecan u plz add a program on how to accept a string and find out the largest word.
ReplyDeleteSir this question is giving a wrong answer plz tell me this I request u sir if u need a number my i will give u...
ReplyDelete