Added findNumOccurrence function

This commit is contained in:
2021-05-28 15:22:12 -04:00
parent 7b9207c387
commit 3adf0c22af

View File

@@ -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;
}