Computer Applications

Write a program by using scanner class to input a sentence. Print each word of the sentence along with the sum of the ASCII codes of its characters.

Java

Input in Java

7 Likes

Answer

import java.util.Scanner;

public class KboatWordSum
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence ending with a space & terminating with (.):");
        while (true) {
            String word = in.next();
            if (word.equals("."))
                break;
            
            int sum = 0;
            for (int i = 0; i < word.length(); i++) {
                sum += (int)word.charAt(i);
            }
            System.out.println(word + "\t" + sum);
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

3 Likes


Related Questions