<?php
//given string
$str = "JIMMY AND JOLLY";
echo "Given String : $str <br>" ;
//convert to character array
$charArray = str_split($str);
//$result["charBooleans"] = array_fill(0, 25, 0);
$result["charBooleans"] = new SplFixedArray(26);
for( $i = 0 , $len = count($charArray) ; $i < $len ; ++$i )
{
if($charArray[$i] == ' ')
continue;
for( $j = $i + 1 ; $j < $len ; ++$j )
{
//check if characters are same
if( $charArray[$i] == $charArray[$j] && $charArray[$i] != ' ' && $result["charBooleans"][$i] != 1 )
{
//ord() converts char to ASCII value
$num = ord($charArray[$i]) - 65;
//echo "num : $num and char $charArray[$i]: <br>";
$result["charBooleans"][$num] = 1;
//the character exist in string hence break from inner loop
break;
}
}
}
for( $i = 0 , $len = count($result["charBooleans"]) ; $i < $len ; ++$i )
{
if($result["charBooleans"][$i] == 1)
{
echo chr(65 + $i) ;
echo "<br>";
}
}
?>
//given string
$str = "JIMMY AND JOLLY";
echo "Given String : $str <br>" ;
//convert to character array
$charArray = str_split($str);
//$result["charBooleans"] = array_fill(0, 25, 0);
$result["charBooleans"] = new SplFixedArray(26);
for( $i = 0 , $len = count($charArray) ; $i < $len ; ++$i )
{
if($charArray[$i] == ' ')
continue;
for( $j = $i + 1 ; $j < $len ; ++$j )
{
//check if characters are same
if( $charArray[$i] == $charArray[$j] && $charArray[$i] != ' ' && $result["charBooleans"][$i] != 1 )
{
//ord() converts char to ASCII value
$num = ord($charArray[$i]) - 65;
//echo "num : $num and char $charArray[$i]: <br>";
$result["charBooleans"][$num] = 1;
//the character exist in string hence break from inner loop
break;
}
}
}
for( $i = 0 , $len = count($result["charBooleans"]) ; $i < $len ; ++$i )
{
if($result["charBooleans"][$i] == 1)
{
echo chr(65 + $i) ;
echo "<br>";
}
}
?>
No comments:
Post a Comment