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.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<T>& ary);
//This function finds the largest element in a vector
template <class T>
T findMax(const std::vector<T>& 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<T>& 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;
}
}