diff --git a/Algorithms.hpp b/Algorithms.hpp index 548b5e2..351192c 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -1,10 +1,10 @@ //myClasses/Algorithms.hpp //Matthew Ellison // Created: 11-08-18 -//Modified: 02-28-19 +//Modified: 07-28-20 //This file contains the declarations and implementations to several algoritms that I have found useful /* - 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 @@ -86,6 +86,8 @@ T findMin(const std::vector& ary); //This function finds the largest element in a vector template T findMax(const std::vector& ary); +//This function returns the number of times the character occurs in the string +int findNumOccurrence(std::string str, char ch); //This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers @@ -479,6 +481,20 @@ T findMax(const std::vector& ary){ return max; } +//This function returns the number of times the character occurs in the string +int findNumOccurrence(std::string str, char ch){ + int num = 0; //Set the number of occurrences to 0 to start + //Loop through every character in the string and compare it to the character passed in + for(char strCh : str){ + //If the character is the same as the one passed in increment the counter + if(strCh == ch){ + ++num; + } + } + //Return the number of times the character appeared in the string + return num; +} + } diff --git a/testAlgorithms.cpp b/testAlgorithms.cpp index b7a108b..318cecb 100644 --- a/testAlgorithms.cpp +++ b/testAlgorithms.cpp @@ -1,10 +1,10 @@ //myClasses/Algorithms.cpp //Matthew Ellison // Created: 11-14-18 -//Modified: 02-28-19 +//Modified: 07-28-20 //This is the file that tests all my algorithms, both for speed and accuracy /* - 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 @@ -51,15 +51,16 @@ bool testQuickSort(); bool testSearch(); bool testFindMin(); bool testFindMax(); +bool testFindNumOccurrence(); int main(){ mee::Stopwatch timer; bool passedTest = false; std::vector functions {testGetPrimes, testGetNumPrimes, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations, - testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax}; + testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence}; std::vector names {"getPrimes", "getNumPrimes", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations", - "getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax"}; + "getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence"}; //Start doing tests and print out the results of each for(int cnt = 0;cnt < functions.size();++cnt){ @@ -424,6 +425,36 @@ bool testFindMax(){ return true; } +bool testFindNumOccurrence(){ + //Test 1 + std::string testString = "abcdefgdd"; + char testChar = 'a'; + int correctAnswer = 1; + int answer = mee::findNumOccurrence(testString, testChar); + if(correctAnswer != answer){ + return false; + } + + //Test 2 + testChar = 'd'; + correctAnswer = 3; + answer = mee::findNumOccurrence(testString, testChar); + if(correctAnswer != answer){ + return false; + } + + //Test 3 + testChar = 'h'; + correctAnswer = 0; + answer = mee::findNumOccurrence(testString, testChar); + if(correctAnswer != answer){ + return false; + } + + //If it hasn't failed a test then return true for passing all the tests + return true; +} + /* Results: Function getPrimes() passed the test The test took 0.000 nanoseconds @@ -466,11 +497,11 @@ The test took 0.000 nanoseconds Function bubbleSort() passed the test -The test took 142.002 milliseconds +The test took 1.031 seconds Function quickSort() passed the test -The test took 1.003 milliseconds +The test took 4.000 milliseconds Function search() passed the test @@ -483,4 +514,8 @@ The test took 0.000 nanoseconds Function findMax() passed the test The test took 0.000 nanoseconds + + +Function findNumOccurrence() passed the test +The test took 0.000 nanoseconds */