From 7099ce1ae6cfa1bc55278a95627b21a14945c2c7 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 28 Jul 2020 01:08:15 -0400 Subject: [PATCH] Added function to count occurrences of a char in a string --- src/main/java/mattrixwv/Algorithms.java | 10 ++++++--- src/test/java/mattrixwv/TestAlgorithms.java | 25 ++++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/java/mattrixwv/Algorithms.java b/src/main/java/mattrixwv/Algorithms.java index 52d9a02..57bb549 100644 --- a/src/main/java/mattrixwv/Algorithms.java +++ b/src/main/java/mattrixwv/Algorithms.java @@ -1,11 +1,11 @@ -//src/main/java/mattrixwv/Algorithms.java +//JavaClasses/src/main/java/mattrixwv/Algorithms.java //Matthew Ellison // Created: 03-02-19 -//Modified: 06-15-20 +//Modified: 07-27-20 //This class holds many algorithms that I have found it useful to keep around //As such all of the functions in here are static and meant to be used as stand alone functions /* -Copyright (C) 2019 Matthew Ellison +Copyright (C) 2020 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -849,4 +849,8 @@ public class Algorithms{ String swappedString = new String(tempStr); return swappedString; } + //This function returns the number of times the character occurs in the string + public static long findNumOccurrence(String str, char c){ + return str.chars().filter(ch -> ch == c).count(); + } } diff --git a/src/test/java/mattrixwv/TestAlgorithms.java b/src/test/java/mattrixwv/TestAlgorithms.java index 303d5fb..77b0e96 100644 --- a/src/test/java/mattrixwv/TestAlgorithms.java +++ b/src/test/java/mattrixwv/TestAlgorithms.java @@ -1,11 +1,11 @@ -//src/test/java/mattrixwv/TestAlgorithms.java +//JavaClasses/src/test/java/mattrixwv/TestAlgorithms.java //Matthew Ellison // Created: 06-07-20 -//Modified: 06-07-20 +//Modified: 07-28-20 //This class holds many algorithms that I have found it useful to keep around //As such all of the functions in here are static and meant to be used as stand alone functions /* -Copyright (C) 2019 Matthew Ellison +Copyright (C) 2020 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -347,4 +347,23 @@ public class TestAlgorithms{ ArrayList answer = Algorithms.getPermutations(permString); assertEquals("getPermutations failed", correctAnswer, answer); } + @Test + public void testFindNumOccurrence(){ + //Test 1 + String testString = "abcdefgdd"; + char testChar = 'a'; + long correctAnswer = 1; + long answer = Algorithms.findNumOccurrence(testString, testChar); + assertEquals(correctAnswer, answer); + //Test 2 + testChar = 'd'; + correctAnswer = 3; + answer = Algorithms.findNumOccurrence(testString, testChar); + assertEquals(correctAnswer, answer); + //Test 3 + testChar = 'h'; + correctAnswer = 0; + answer = Algorithms.findNumOccurrence(testString, testChar); + assertEquals(correctAnswer, answer); + } }