Computer Science

The encryption of letters 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 letters.

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 ~~alphabetical~~ ~~order~~ increasing order of their potential. Output the result in the 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

Java

Java Arrays

14 Likes

Answer

import java.util.*;

public class KboatWordPotential
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("ENTER THE SENTENCE:");
        String ipStr = in.nextLine();
        int len = ipStr.length();

        char lastChar = ipStr.charAt(len - 1);
        if (lastChar != '.' 
        && lastChar != '?' 
        && lastChar != '!') {
            System.out.println("INVALID INPUT");
            return;
        }

        String str = ipStr.substring(0, len - 1);
        StringTokenizer st = new StringTokenizer(str);

        int wordCount = st.countTokens();
        String strArr[] = new String[wordCount];
        int potArr[] = new int[wordCount];

        for (int i = 0; i < wordCount; i++) {
            strArr[i] = st.nextToken();
            potArr[i] = computePotential(strArr[i]);
        }

        System.out.println("Potential:");
        for (int i = 0; i < wordCount; i++) {
            System.out.println(strArr[i] + " = " + potArr[i]);
        }

        /*
         * Sort potential array and words array
         * as per potential array
         */
        for (int i = 0; i < wordCount - 1; i++) {
            for (int j = 0; j < wordCount - i - 1; j++) {
                if (potArr[j] > potArr[j+1]) {
                    int t = potArr[j];
                    potArr[j] = potArr[j+1];
                    potArr[j+1] = t;

                    String temp = strArr[j];
                    strArr[j] = strArr[j+1];
                    strArr[j+1] = temp;
                }
            }
        }

        System.out.println("Sorted Sentence");
        for (int i = 0; i < wordCount; i++) {
            System.out.print(strArr[i] + " ");
        }
    }

    public static int computePotential(String word) {
        String str = word.toUpperCase();
        int p = 0;
        int l = str.length();
        /*
         * Substracting 64 from ASCII Value of
         * letter gives the proper potentials:
         * A => 65 - 64 = 1
         * B => 66 - 64 = 2
         * ..
         * ..
         */
        for (int i = 0; i < l; i++) {
            p += str.charAt(i) - 64;
        }

        return p;
    }
}

Output

Answered By

3 Likes


Related Questions