How can I change the value of the duplicated character in a string?
if they are duplicated I want to have a specific value for that else if they are not, I also want to put a specific value for that too.
Scenario 1:
Input: hello
Output: ..??.
Scenario 2:
Input: elephant
Output: ?.?….
$str = "elephant";
$arr= str_split($str);
for($i = 0; $i < count($arr); $i++) {
for($j = $i + 1; $j < count($arr); $j++) {
if($arr[$i] == $arr[$j]){
}
}
}
I really don’t know on how will I change the value of it then implode them again.
>Solution :
try this:
$str = "hello";
$arr = str_split($str);
$duplicates = array_unique(array_diff_assoc($arr,array_unique($arr)));
for($i = 0; $i < strlen($str); $i++) {
if(in_array($str[$i], $duplicates)) {
$str[$i] = '?';
} else {
$str[$i] = '.';
}
}
echo $str;