Program opened below. You can scroll through the algorithm, Java code and output.
Program 21
Alphabetic Potential of a String
Question
Read text ending in . or ?. Add A=1 through Z=26 for every alphabetic character, ignoring punctuation, digits and spaces.
Algorithm
Step 1: Start and create a Scanner.
Step 2: Read a line of text.
Step 3: Initialize the potential sum to zero.
Step 4: Begin at the first character of the line.
Step 5: Read the current character.
Step 6: Check for the terminating full stop or question mark.
Step 7: If a terminator is found, finish the scanning loop.
Step 8: Convert alphabetic characters to uppercase for a common calculation.
Step 9: Test whether the character lies between A and Z.
Step 10: If it is alphabetic, subtract the code for A and add one.
Step 11: Add this alphabet position to the running total.
Step 12: Ignore digits, symbols and spaces.
Step 13: Continue with the next character.
Step 14: Display the accumulated potential.
Step 15: Stop.
Java program
Javaimport java.util.*;
class StringPotential {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int sum = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == '.' || ch == '?') {
break;
}
ch = Character.toUpperCase(ch);
if (ch >= 'A' && ch <= 'Z') {
sum += ch - 'A' + 1;
}
}
System.out.println("Potential = " + sum);
}
}Sample output
Input: Did7 @Ate &9 ?
Output: Potential = 43