In this program, we will learn to code the Java Program to Find the Potential of a Word. Let’s understand First What is the Potential of a Word in Java? and How to Find the Potential of a Word in Java Programming Language?
Potential of a Word in Java
Let’s see How to find the Potential of a Word in Java?
The Potential of a word is found by adding the ASCII value of the alphabets. ASCII values of Alphabet A to Z are 65 to 90.
Let’s understand by the following example,
Word = CODE
Potential of the word is 67 + 79 + 68 + 69 = 283.
Potential of word BALL will be 66 + 65 + 76 + 76 = 283.
Problem Statement
Write a Program to accept a sentence that may be terminated by either “.” , “?” or “!” only. The words of sentences are separated by a single space and are in UPPERCASE. 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
Java Program to Find the Potential of a Word
import java.util.*; public class Main { 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 Input"); } else if(!(myString.toUpperCase()).equals(myString)) { System.out.println("Invalid Input - Not in upper case"); } else { 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]+" - "+ 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( getPotential(strArray[j])> 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 1
Enter any Sentence in upper case:
HOW DO YOU DO?
HOW - 238
DO - 147
YOU - 253
DO - 147
DO DO HOW YOU
Output 2
Enter any Sentence in upper case:
LOOK BEFORE YOU LEAP.
LOOK - 309
BEFORE - 435
YOU - 253
LEAP - 290
YOU LEAP LOOK BEFORE
Output 3
Enter any Sentence in upper case:
HOW ARE YOU#
Invalid Input
This is the Java Program to Find the Potential of a Word.
Conclusion
I hope after going through this post, you understand the Java Program to Find the Potential of a Word. If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: