given a string input1,having alphabets or digits or both ,find the sum of digits or only the characters depending on input2 prototype string findchars or DigitSum(String input1,input2) input1-String having alphabets or digits or both input2-either 0 or 1 if input2=0,find the sum of digits from string input1 if input2 =1 ,extract only alphabets from string input1 .
rules:
if there are no digits in the given string and input2 is 0 ,result would be zero.
Answers
Answered by
0
Answer:
class GFG {
static int findSum(String str)
{
String temp = "0";
int sum = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isDigit(ch))
temp += ch;
else {
sum += Integer.parseInt(temp);
temp = "0";
}
}
return sum + Integer.parseInt(temp);
}
public static void main(String[] args)
{
String str = "12abc20yz68";
System.out.println(findSum(str));
}
}
#SPJ2
Similar questions
Math,
1 month ago
Science,
1 month ago
Computer Science,
1 month ago
Math,
2 months ago
Computer Science,
9 months ago
Biology,
9 months ago