Arrange Words based on their Potential

Write a program in Java to allow the user to enter a sentence that may only terminate with a ‘.’, ‘?’ or a ‘!’.

For an invalid sentence, display a suitable error message and exit the program.

It is assumed that the words in the sentence are separated with a single blank space.

Convert the sentence into uppercase.

Now arrange the words in the sentence in ascending order, based on their potential.

The potential of a word is found by summing up the ASCII values of the individual letters of the word.

For example, the potential of the word “BALL” = 66 + 65 + 76 + 76 = 283.

Display the words in ascending order of their potential.

Example:
INPUT: HOW DO YOU DO?
OUTPUT:
HOW = 238
DO = 147
YOU = 253
DO = 147
DO DO HOW YOU

import java.io.*;
import java.util.StringTokenizer;
class Potential{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Sentence: ");
        String s = br.readLine();
        int len = s.length();
        char last = s.charAt(len - 1);
        if(last != '!' && last != '?' && last != '.'){
            System.out.println("Invalid sentence!");
            return;
        }
        s = s.toUpperCase();
        StringTokenizer st = new StringTokenizer(s, " !?.,");
        int count = st.countTokens();
        String words[] = new String[count];
        int p[] = new int[count];
        for(int i = 0; i < count; i++){
            words[i] = st.nextToken();
            p[i] = potential(words[i]);
            System.out.println(words[i] + " = " + p[i]);
        }
        for(int i = 0; i < count; i++){
            for(int j = 0; j < count - 1 - i; j++){ 
                if(p[j] > p[j + 1]){
                    int temp = p[j];
                    p[j] = p[j + 1];
                    p[j + 1] = temp;
                    String t = words[j];
                    words[j] = words[j + 1];
                    words[j + 1] = t;
                }
            }
        }
        for(int i = 0; i < count; i++)
            System.out.print(words[i] + " ");
        System.out.println();
    }
    public static int potential(String w){
        int p = 0;
        for(int i = 0; i < w.length(); i++)
            p += w.charAt(i);
        return p;
    }
}

Write a program in Java to accept a sentence which is terminated by either ‘.’, ‘?’ or ‘!’. For an invalid sentence, display a suitable error message. Each word of the sentence is separated by single blank space.

Decode the words according to their potential and arrange them in ascending order. The potential of a word is formed by adding the encrypted value of the alphabet.

For example:
KITE
Potential = 11 + 9 + 20 + 5 = 45.
A = 1, B = 2, C = 3, … Z = 26.

Display the sorted list of words.

import java.io.*;
import java.util.StringTokenizer;
class Potential{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the sentence: ");
        String s = br.readLine();
        s = s.trim();
        char last = s.charAt(s.length() - 1);
        if(last != '.' && last != '?' && last != '!'){
            System.out.println("Invalid sentence!");
            return;
        }
        s = s.toUpperCase();
        StringTokenizer st = new StringTokenizer(s, " .,;?!");
        int count = st.countTokens();
        int p[] = new int[count];
        String w[] = new String[count];
        for(int i = 0; i < count; i++){
            w[i] = st.nextToken();
            p[i] = potential(w[i]);
        }
        for(int i = 0; i < count; i++){
            for(int j = 0; j < count - 1 - i; j++){
                if(p[j] > p[j + 1]){
                    int temp = p[j];
                    p[j] = p[j + 1];
                    p[j + 1] = temp;
                    String t = w[j];
                    w[j] = w[j + 1];
                    w[j + 1] = t;
                }
            }
        }
        System.out.println("Arranged words:");
        for(int i = 0; i < count; i++)
            System.out.print(w[i] + "\t");
        System.out.println();
    }
    public static int potential(String w){
        int p = 0;
        for(int i = 0; i < w.length(); i++){
            char ch = w.charAt(i);
            p += ch - 'A' + 1;
        }
        return p;
    }
}

5 thoughts on “Arrange Words based on their Potential”

  1. WAP in Java to enter a natural number ,where N>100 and N<1000,the natural number must not contain zeros. Print all the combinations of the digits of the number including the number itself. Each new combination should appear on a new line.
    Sample Input:
    N=465
    Sample Output:
    456
    465
    546
    564
    645
    654

  2. Sir,in the above potential program can we write
    String s=br.readLine().trim();?
    In order to remove extra space in the sentence

  3. Sir,a program is given which states that:
    Given a square matrix M [ ] [ ] of order ‘n’. The maximum value possible for ‘n’ is 10. Accept three
    different characters from the keyboard and fill the array according to the instruction given below.
    Fill the upper and lower elements formed by the intersection of the diagonals by character 1.
    Fill the left and right elements formed by the intersection of the diagonals by character 2.
    Fill both the diagonals by character 3.
    Output the result in format given below:
    ENTER SIZE : 4
    INPUT : FIRST CHARACTER ‘*’
    SECOND CHARACTER ‘?’
    THIRD CHARACTER ‘#’
    OUTPUT :

    # * * #
    ? # # ?
    ? # # ?
    # * * #

Leave a Reply

%d bloggers like this: