Added function to count occurrences of a char in a string

This commit is contained in:
2020-07-28 01:08:14 -04:00
parent 077e265de3
commit 1ee5d04871
2 changed files with 59 additions and 8 deletions

View File

@@ -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<boolFn> 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<std::string> 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
*/