count occurrences of a character
in a string : case-sensitive
[ split | length ]

Count occurrences of a given character in a string

Write a function that takes a string and returns the number of occurrences of a given single character found inside that string.


Example ...

Enter a string and a character to count ...

"Many moons ago" string

"o" character to count

The function will return ↴

3 number of case-insensitive occurrences of given character found inside the string.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Case sensitivity defines whether uppercase and lowercase letters are treated as distinct or equivalent.

case-sensitive will distinguish between uppercase and lowercase letters.

"A" will be treated as distinct to the letter "a" and vice versa. They will be treated as different.


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Count occurrences of a character in a string using the split() method.


split() method splits a string into an array of substrings based on a specified separator (delimiter). The original string is unchanged.

("") separator → string is split between each character.

(" ") separator → string is split at each space character, resulting in an array of words.

const str3 = "Hello"; → string

str3.split(""); returns ↴

["H", "e", "l", "l", "o"] → array

const str4 = "hello world"; → string

str4.split(" "); returns ↴

["hello", "world"] → array


length property returns the number of characters in a string.

const str5 = "Hello World";

str5.length; returns ↴

11 → there are 11 characters in the string

Strings are zero indexed, the first character will be index 0

The last character will be at index length - 1

To find the last index in a string.

const str6 = "Hello World";

str6.length - 1; returns ↴

10 → end index of string


Initialize a variable to hold the string to be searched.

const string1 = "HELLO WORLD, hello world"; → user input

Initialize a variable to hold the character to count.

const char1 = "o"; → user input


Define a function countOccurrences to count occurrences of a character in a string.

function countOccurrences(str, char) {}

The function takes two parameters ↴

string1 the string to be searched, str

char1 the character to count, char

The function returns the number of occurrences of the specified character found in the string.

split() method splits a string into an array of substrings based on a specified separator.

The character that we want to count is used as the separator.

const str = "Hello World" string to search

const char = "o" "o" separator

str.split(char) returns ↴

['Hell', ' W', 'rld'] array with 3 substrings

The string will be split wherever it finds the character "o"

And will remove the separator character "o" from the string.

length property of an Array instance represents the number of elements in that array.

The number of occurrences of the character is one less than the number of substrings.

Subtract 1 from the length to get the final count and return.

return str.split(char).length - 1


Call the function with ↴

countOccurrences(string1, char1);


Count occurrences of the character "o" in a string | case-sensitive

const string1 = "HELLO WORLD, hello world";

const char1 = "o";

function countOccurrences(str, char) {

return str.split(char).length - 1;

}

call function

countOccurrences(string1, char1); returns ↴

2


Alternative using an arrow function ↴

const countOccurrences2 = (str, char) => str.split(char).length - 1;

countOccurrences2(string1, char1);

Count occurrences of a given character in a string

character to count