From 02cf2cd62772e00f8484e51886eede4585d924b3 Mon Sep 17 00:00:00 2001 From: Ravikant Patidar Date: Mon, 9 Sep 2024 17:20:17 +0530 Subject: [PATCH] add another approach using regular expression --- problems/string/find_sum_from_string.md | 5 ---- .../string/find_sum_from_string/Approach.java | 0 .../find_sum_from_string/Approach2.java | 27 +++++++++++++++++++ .../string/find_sum_from_string/README.md | 5 ++++ 4 files changed, 32 insertions(+), 5 deletions(-) delete mode 100644 problems/string/find_sum_from_string.md rename solutions/string/find_sum_from_string.java => problems/string/find_sum_from_string/Approach.java (100%) create mode 100644 problems/string/find_sum_from_string/Approach2.java create mode 100644 problems/string/find_sum_from_string/README.md diff --git a/problems/string/find_sum_from_string.md b/problems/string/find_sum_from_string.md deleted file mode 100644 index 54ca6a2..0000000 --- a/problems/string/find_sum_from_string.md +++ /dev/null @@ -1,5 +0,0 @@ -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 diff --git a/solutions/string/find_sum_from_string.java b/problems/string/find_sum_from_string/Approach.java similarity index 100% rename from solutions/string/find_sum_from_string.java rename to problems/string/find_sum_from_string/Approach.java diff --git a/problems/string/find_sum_from_string/Approach2.java b/problems/string/find_sum_from_string/Approach2.java new file mode 100644 index 0000000..a398de1 --- /dev/null +++ b/problems/string/find_sum_from_string/Approach2.java @@ -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; + } +} \ No newline at end of file diff --git a/problems/string/find_sum_from_string/README.md b/problems/string/find_sum_from_string/README.md new file mode 100644 index 0000000..204a8b5 --- /dev/null +++ b/problems/string/find_sum_from_string/README.md @@ -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`