mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added function to count occurrences of a char in a string
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user