Skip to content

Commit 6436a17

Browse files
author
omprakash umath
committed
Java interview question
1 parent 7de6578 commit 6436a17

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
03Ju23st 15te5ll10 12me6 3the ti45me 09f567or 06bu7s 23
2+
From the Given string print sum of the numbers given in the string
3+
3+23+15+5+10+12+6+3+45+9+567+6+7+23=734
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class FindSum {
2+
public static void main(String[] args) {
3+
String str="03Ju23st 15te5ll10 12me6 3the ti45me 09f567or 06bu7s 23";
4+
int num=0;
5+
int sum=0;
6+
String numStr="";
7+
for(int i=0;i<str.length();i++){
8+
if(str.charAt(i)>='0' && str.charAt(i)<='9'){
9+
numStr+=str.charAt(i);
10+
}else{
11+
if(numStr.length()>0)
12+
num=Integer.parseInt(numStr);
13+
sum=sum+num;
14+
numStr="";
15+
}
16+
}
17+
if(numStr.length()>0)
18+
{
19+
num=Integer.parseInt(numStr);
20+
sum=sum+num;
21+
}
22+
System.out.println(sum);
23+
}
24+
}

0 commit comments

Comments
 (0)