ISC Practicals and Theory - Word Potential Sort
Updated 21 Oct 2026
This program reads a sentence, separates it into words, finds the potential of every word (the sum of the alphabet positions of its letters) and prints the words in ascending order of potential. The words are extracted with charAt(), indexOf(), length() and substring() only, and the sorting is written by hand. Everything is written in the main method.
Definition
The potential of a word is the sum of the positions of its letters in the alphabet: A = 1, B = 2, ... Z = 26. Capital and small letters count the same, so a and A are both 1.
Abhas potential 1 + 2 = 3, andbahas potential 2 + 1 = 3.Zoohas potential 26 + 15 + 15 = 56.programminghas potential 16 + 18 + 15 + 7 + 18 + 1 + 13 + 13 + 9 + 14 + 7 = 131.
In this program a character that is not a letter (a digit, a comma, a hyphen) is ignored, that is, it adds 0. So Hi, has potential 8 + 9 = 17 and 12 has potential 0.
The sentence must end with ., ? or !, after the spaces at the two ends of the line have been removed with trim(). That terminator is not part of any word. Words are separated by one or more spaces. Any other punctuation inside the sentence stays attached to its word.
If two words have the same potential, they stay in the order in which they were typed. This is called a stable sort.
Algorithm
- Read the sentence into
sentand remove the spaces at its two ends withtrim(). If it is empty, or does not end in.,?or!, printINVALID INPUTand stop. - Remove the terminator:
s = sent.substring(0, len - 1). Create the arraysw(the words) andp(their potentials), both of size 50, and setn = 0. - Cut out the words: with
ifrom 0, skip a space; otherwisee = s.indexOf(' ', i)is the space that ends the word (or the end of the sentence if there is none). Ifnis already 50, settooMany = trueand stop; otherwise stores.substring(i, e)inw[n], add 1 tonand continue fromi = e. - If
tooManyis true, print the message that 50 words is the maximum. If no word was found (n == 0), printINVALID INPUT. - Find the potential of every word: add
c - 'A' + 1for each capital lettercandc - 'a' + 1for each small letter. Characters that are not letters (digits, commas) are ignored. Store the sum inp. - Print every word with its potential.
- Sort with an insertion sort: take the word
kwwith potentialkpand shift every greater potential one place to the right (p[j] > kp), then place the word in the gap. A word moves left only past a strictly greater potential, so words of equal potential keep their original order. - Print every word with its potential again, now in ascending order.
Program
The while loop in main extracts the words, one loop finds the potentials, and the insertion sort arranges the arrays w and p together.
import java.util.Scanner;
/* Program WordPotential reads a sentence, separates it into words without
split() or StringTokenizer, finds the potential of every word (sum of the
alphabet positions of its letters) and prints the words in ascending order
of potential. */
public class WordPotential
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sent = sc.nextLine().trim(); // trim() removes the spaces at the two ends
int len = sent.length();
boolean valid = false; // true if the sentence ends with . ? or !
if (len > 0) // an empty line is invalid
{
char last = sent.charAt(len - 1); // the terminator
valid = last == '.' || last == '?' || last == '!';
}
if (!valid)
{
System.out.println("INVALID INPUT");
return;
}
String s = sent.substring(0, len - 1); // sentence without terminator
int sl = s.length();
String w[] = new String[50]; // the words of the sentence (at most 50)
int p[] = new int[50]; // p[i] stores the potential of the word w[i]
int n = 0; // number of words extracted
boolean tooMany = false; // true if the sentence has more than 50 words
int i = 0; // index of the character being examined
while (i < sl)
{
if (s.charAt(i) == ' ') // extra space: skip it
i++;
else
{
int e = s.indexOf(' ', i); // the space that ends this word
if (e == -1) // no space left: word ends with the sentence
e = sl;
if (n == 50) // array is full
{
tooMany = true;
break;
}
w[n] = s.substring(i, e); // the word, from i up to e - 1
n++;
i = e; // continue after the word
}
}
if (tooMany)
System.out.println("Too many words. Maximum 50 words allowed.");
else if (n == 0)
System.out.println("INVALID INPUT");
else
{
for (int a = 0; a < n; a++) // potential of every word; other characters are ignored
{
int sum = 0;
for (int b = 0; b < w[a].length(); b++)
{
char c = w[a].charAt(b);
if (c >= 'A' && c <= 'Z')
sum = sum + (c - 'A' + 1); // capital letter
else if (c >= 'a' && c <= 'z')
sum = sum + (c - 'a' + 1); // small letter
}
p[a] = sum;
}
System.out.println("Words with their potential:");
for (int a = 0; a < n; a++)
System.out.println(w[a] + " = " + p[a]);
// insertion sort: a word moves left only past a strictly greater potential,
// so words of equal potential keep their original order (stable sort)
for (int a = 1; a < n; a++)
{
String kw = w[a]; // word being inserted
int kp = p[a]; // its potential
int j = a - 1;
while (j >= 0 && p[j] > kp) // shift greater potentials one place right
{
w[j + 1] = w[j];
p[j + 1] = p[j];
j--;
}
w[j + 1] = kw; // place the word in its position
p[j + 1] = kp;
}
System.out.println("Words in ascending order of potential:");
for (int a = 0; a < n; a++)
System.out.println(w[a] + " = " + p[a]);
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
sent | String | The sentence typed by the user, with its terminator, after trim() |
len | int | Length of sent |
valid | boolean | True if the sentence is not empty and ends with ., ? or ! |
last | char | The last character of the sentence, the terminator |
s | String | The sentence without its terminator |
sl | int | Length of s |
w | String[] | Stores the words of the sentence (at most 50) |
p | int[] | p[i] stores the potential of the word w[i] |
n | int | Number of words extracted |
tooMany | boolean | True if the sentence has more than 50 words |
i | int | Index of the character being examined while cutting out the words |
e | int | Index of the space that ends the current word |
a | int | Loop counter over the words |
sum | int | Potential of the word being measured |
b | int | Loop counter over the letters of a word |
c | char | The letter being added to the potential |
kw | String | The word being inserted in the insertion sort |
kp | int | The potential of kw |
j | int | Index used to shift greater potentials to the right |
Sample input and output
Sample 1
Enter a sentence: I love Java programming.
Words with their potential:
I = 9
love = 54
Java = 34
programming = 131
Words in ascending order of potential:
I = 9
Java = 34
love = 54
programming = 131Sample 2
Enter a sentence: Ab ba cd Dc a Zoo!
Words with their potential:
Ab = 3
ba = 3
cd = 7
Dc = 7
a = 1
Zoo = 56
Words in ascending order of potential:
a = 1
Ab = 3
ba = 3
cd = 7
Dc = 7
Zoo = 56Sample 3
Enter a sentence: Go?
Words with their potential:
Go = 22
Words in ascending order of potential:
Go = 22Sample 4
Enter a sentence: Hello
INVALID INPUTSample 5
Enter a sentence: Hi, ok 12.
Words with their potential:
Hi, = 17
ok = 26
12 = 0
Words in ascending order of potential:
12 = 0
Hi, = 17
ok = 26Sample 6
Enter a sentence: Go?
Words with their potential:
Go = 22
Words in ascending order of potential:
Go = 22Dry run
Input: Ab ba cd Dc a Zoo! (two spaces at the start). The program trims them, so sent is Ab ba cd Dc a Zoo!. The terminator ! is removed, so s has 21 characters.
Extraction (i is where a word starts, e where it ends):
i | e | w[n] is set to | n after |
|---|---|---|---|
| 0 | 2 | Ab | 1 |
| 5 | 7 | ba | 2 |
| 8 | 10 | cd | 3 |
| 12 | 14 | Dc | 4 |
| 15 | 16 | a | 5 |
| 18 | -1, so 21 | Zoo | 6 |
Potentials: Ab = 3, ba = 3, cd = 3 + 4 = 7, Dc = 4 + 3 = 7, a = 1, Zoo = 56. So p = 3, 3, 7, 7, 1, 56.
Insertion sort (p after each value of i):
a | kw, kp | Shifts | Order after the step |
|---|---|---|---|
| 1 | ba, 3 | none (3 is not greater than 3) | Ab, ba, cd, Dc, a, Zoo |
| 2 | cd, 7 | none | Ab, ba, cd, Dc, a, Zoo |
| 3 | Dc, 7 | none (7 is not greater than 7) | Ab, ba, cd, Dc, a, Zoo |
| 4 | a, 1 | Dc, cd, ba, Ab move right | a, Ab, ba, cd, Dc, Zoo |
| 5 | Zoo, 56 | none | a, Ab, ba, cd, Dc, Zoo |
The final order is a = 1, Ab = 3, ba = 3, cd = 7, Dc = 7, Zoo = 56. This is the same as the real run of the second sample above, and the equal potentials Ab, ba and cd, Dc kept their typed order.
Variations
- Sort by length "Write a program to read a sentence and print its words in ascending order of length."
- Alphabetical order "Write a program to arrange the words of a sentence in alphabetical order."
- Highest potential "Write a program to print the word of a sentence that has the highest potential."
- Palindrome words "Write a program to count the words of a sentence that are palindromes."