Monday, 6 January 2014

Java: WAP to print the sum of characters in a String. Assuming that A=1, B=2, C=3.....So DAD will be 4+1+4=9.

import java.io.*;
import java.io.InputStreamReader;
//import sun.security.util.Length;


public class HelloWorld{

     public static void main(String []args){
       // System.out.println("Hello World");
       int sum=0;
       String br="DAD";
 
      char[] word=br.toLowerCase().toCharArray();
       for(int i :word)
       {
           sum=sum +i -96;
       }
       System.out.println("Sum of characters:"+sum);
     }
}




No comments:

Post a Comment