Programming Examples

Java program to find the potential of a word is found by adding the ASCII value of the alphabets


The potential of a word is found by adding the ASCII value of the alphabets.

(ASCII values of A to Z are 65 to 90).

Example: BALL

Potential = 66 + 65 + 76 + 76 = 283

Write a program to accept a sentence which may be terminated by either “ . ” , “ ? ” or “ ! ” only. The words of sentence are separated by single blank space and are in UPPER CASE. Decode the words according to their potential and arrange them in ascending order of their potential strength.

Test your program with the following data and some random data:

Example 1:

INPUT: HOW DO YOU DO?

OUTPUT: HOW = 238

DO = 147

YOU = 253

DO = 147

DO DO HOW YOU

Example 2:

INPUT: LOOK BEFORE YOU LEAP.

OUTPUT: LOOK = 309

BEFORE = 435

YOU = 253

LEAP = 290

YOU LEAP LOOK BEFORE

Example 3:

INPUT: HOW ARE YOU#

OUTPUT: INVALID INPUT

Solution

import java.util.*;
class Potential
{
    static int getPotential(String str)
    {
        int sum=0;
        for(int i=0;i<str.length();i++)
        {
            sum=sum+(int)(str.charAt(i));
        }
        return sum;
    }
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any Sentence in upper case ");
        String myString=sc.nextLine();
        if(!(myString.endsWith("?")||myString.endsWith(".")||myString.endsWith("!")))
        {
            System.out.println("Invalid String");
        }
        else if(!(myString.toUpperCase()).equals(myString))
        {
            System.out.println("Invalid String - Not in upper case");
        }
        else
        {
            //System.out.println(Potential.getPotential(myString));
            String strArray[]=new String [100];
            String tmpStr="";
            int a=0;
            for(int i=0;i<myString.length();i++)
            {
                if(myString.charAt(i)==' '||myString.charAt(i)=='.'||myString.charAt(i)=='?'||myString.charAt(i)=='!')
                {
                    strArray[a]=tmpStr;
                    tmpStr="";                    
                    System.out.println(strArray[a]+" - "+Potential.getPotential(strArray[a]));
                    a++;
                }
                else
                {
                    tmpStr=tmpStr+myString.charAt(i);
                }                
            }
      
            for(int i=0;i<a;i++)
            {
                
                for(int j=0;j<a-1-i;j++)
                {
                    if(Potential.getPotential(strArray[j])>Potential.getPotential(strArray[j+1]))
                    {
                        tmpStr=strArray[j];
                        strArray[j]=strArray[j+1];
                        strArray[j+1]=tmpStr;
                    }
                }
            }
            
            for(int i=0;i<a;i++)
            {
                System.out.print(strArray[i]+" ");
            }
        }
    }
}
Output

Enter any Sentence in upper case 

HOW DO YOU DO?

HOW - 238

DO - 147

YOU - 253

DO - 147

DO DO HOW YOU 

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz