From 3adf0c22afe88fc4ddff57b10b9af19d442067e8 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 28 May 2021 15:22:12 -0400 Subject: [PATCH] Added findNumOccurrence function --- Algorithms.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Algorithms.ts b/Algorithms.ts index 0d86108..39e88d2 100644 --- a/Algorithms.ts +++ b/Algorithms.ts @@ -605,3 +605,17 @@ function swapString(str: string, first: number, second: number): string{ return tempStr; } + +//This function returns the number of times the character occurs in the string +export function findNumOccurrence(str: string, ch: string){ + let num: number = 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(let strCh of 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; +}