Skip to content

add another approach using regular expression #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions problems/string/find_sum_from_string.md

This file was deleted.

27 changes: 27 additions & 0 deletions problems/string/find_sum_from_string/Approach2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import java.util.Arrays;
import java.util.Scanner;

/**
* this approach use sting method to split string, base on nonNumber string as
* delimiter
*/
class FindSumFromString {
public static void main(String[] args) {
// read string from stdout
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();

System.err.println(sum(str));
}

static int sum(String str) {
String strArr[] = str.split("[^0-9]+");
int sum = 0;
// System.out.println(Arrays.toString(strArr) + str);
for (String numString : strArr) {
sum += Integer.parseInt(numString);
}
return sum;
}
}
5 changes: 5 additions & 0 deletions problems/string/find_sum_from_string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`03Ju23st 15te5ll10 12me6 3the ti45me 09f567or 06bu7s 23`

From the Given string print sum of the numbers given in the string

`3+23+15+5+10+12+6+3+45+9+567+6+7+23=734`
Loading