PHP Program to Find Missing Characters to Make a String Pangram
Last Updated :
27 Dec, 2023
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
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 ) {
$str = strtolower ( $str );
$alphabet = range( 'a' , 'z' );
$present_chars = [];
$missing_chars = '' ;
for ( $i = 0; $i < strlen ( $str ); $i ++) {
$char = $str [ $i ];
if (ctype_alpha( $char )) {
$present_chars [ $char ] = true;
}
}
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 ;
}
$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
Share your thoughts in the comments
Please Login to comment...