public class Sum_Digits_String { public int sum_digits(String str) { int l = str.length(); int sum = 0; for (int i = 0; i < l; i++) { if (Character.isDigit(str.charAt(i))) { String tmp = str.substring(i,i+1); sum += Integer.parseInt(tmp); } } return sum; } public static void main (String[] args) { Sum_Digits_String m = new Sum_Digits_String(); String str = "a1b23c3d4e5hf"; System.out.println("Given String : "+str); System.out.println("Sum of Digits in String : "+m.sum_digits(str)); } }
Given String : a1b23c3d4e5hf Sum of Digits in String : 18
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions