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;
    }
}

4 thoughts on “Arrange Words based on their Potential”

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

  2. 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: