Menu
Coddy logo textTech

Challenge: Character Frequency

Part of the Logic & Flow section of Coddy's PHP journey — lesson 66 of 68.

challenge icon

Challenge

Easy

You will receive a single input: a string of text.

The input will be a string in this format: hello world

Read the input string. Count the frequency of each character in the string (case-insensitive), excluding spaces. Store the character frequencies in an associative array where the keys are the characters and the values are their counts.

Sort the associative array by key in ascending alphabetical order using ksort().

Print each character and its frequency in the following format:

[character]: [count]

Print each character-frequency pair on a separate line, in alphabetical order.

Input format:

  • A single line containing a string of text (example: hello world)

Expected output: Each character and its frequency printed on a separate line in the format: [character]: [count], sorted alphabetically by character

Try it yourself

<?php
// Read input
$text = trim(fgets(STDIN));

// TODO: Write your code below
// 1. Convert the string to lowercase
// 2. Count character frequencies (excluding spaces)
// 3. Sort the array by key using ksort()
// 4. Print each character and its frequency

?>

All lessons in Logic & Flow