Sum of Digits in a given String
Source code in Java:
public class Demo
{
public static void main(String arg[])
{
String s = "good23bad4";
int sum = 0;
for (char c : s.toCharArray())
{
if (Character.isDigit(c))
{
sum += Character.getNumericValue(c);
}
}
System.out.println("Total Sum:"+sum); // Final sum
}
}
Output:
Total Sum:9
Comments
Post a Comment