PHP Program to Find Missing Characters to Make a String Pangram
In this article, we will see how to find the missing characters to make the string Pangram using PHP. A pangram is a sentence containing every letter in the English Alphabet.
Examples:
Input: The quick brown fox jumps over the dog
Output: Missing Characters - alyz
Input: The quick brown fox jumps
Output: Missing Characters - adglvyz
Table of Content
- Using for() Loop
- Using range() and strpos() Methods
PHP Program to Find Missing Characters to Make a String Pangram using for() Loop
The provided PHP code defines a function findMissingCharacters() that takes a string as input and identifies the missing characters required to make it a pangram.
Example: In this example, we will find the missing number to make string pangram.
PHP
<?php function findMissingCharacters( $str ) { // Convert the string to lowercase $str = strtolower ( $str ); // Create an array of all lowercase letters $alphabet = range( 'a' , 'z' ); $present_chars = []; $missing_chars = '' ; // Check each character in the string for ( $i = 0; $i < strlen ( $str ); $i ++) { $char = $str [ $i ]; if (ctype_alpha( $char )) { $present_chars [ $char ] = true; } } // Check for missing characters foreach ( $alphabet as $letter ) { if (!isset( $present_chars [ $letter ])) { $missing_chars .= $letter ; } } return $missing_chars ; } $str = "The quick brown fox jumps over the dog" ; $missing_chars = findMissingCharacters( $str ); if ( empty ( $missing_chars )) { echo "The string is a pangram.\n" ; } else { echo "Missing characters: $missing_chars\n" ; } ?> |
Output
Missing characters: alyz
PHP Program to Find Missing Characters to Make a String Pangram using range() and strpos() Methods
The range() and strpos() methods are used to find the missing number to make the string pangram.
Example:
PHP
<?php function missingChars( $str ) { $result = "" ; $str = strtolower ( $str ); $letters = range( 'a' , 'z' ); foreach ( $letters as $char ) { if ( strpos ( $str , $char ) === false) { $result .= $char ; } } return $result ; } // Driver code $inputString = "The quick brown fox jumps over the dog" ; $missingChars = missingChars( $inputString ); if ( $missingChars === "" ) { echo "String is a pangram" ; } else { echo "Missing characters to make the" . " string pangram: {$missingChars}" ; } ?> |
Output
Missing characters to make the string pangram: alyz
Contact Us