- Example 1:
Input: jh2gv1b78bm9 Output: 2+1+7+8+9= 27
- Approach 1:
Java
public class SumInt
{
public int sumInt(String str) {
int aa = 0;
for (int i = 0; i < str.length(); i++) {
int xx = (int) str.charAt(i);
String zz = "" + str.charAt(i) + "";
if (xx > 47 && xx < 58) {
aa += Integer.parseInt(zz);
}
}
return aa;
}
public static void main(String args[]) {
String str= "jh2gv1b78 bm9";
SumInt obj = new SumInt();
int a = obj.sumInt(str);
System.out.println(a);
}
}
No comments:
Post a Comment