Create a function called that accepts 2 string arguments and returns an integer of the count of occurrences the 2nd argument is found in the first ...
For further actions, you may consider blocking this person and/or reporting abuse
TypeScript
JavaScript
Java:
public static int strCount(String str, char letter) {
int count = 0;
for (char element : str.toCharArray()) {
if (element == letter) {
count++;
}
}
return count;
}
Love it!
JS using a different approach, recursive function
The readable version
One line version
Actually,
string[index out of bound]
will returnundefined
so you don't need to check that your index is lower than the length of the string.Also, you don't need to keep track of the characters' count if you just return it right in your recursive call.
And you can increment your index right when you access your character for comparison.
If you are about the bytes, this makes it for a shorter version of your solution.
You are right, thanks!
Python one liner,
Output,
Kotlin.
Swift solution :
C#
Python 3
JavaScript: