Q:

Write a PHP program to find the first non-repeated character in a given string

0

Write a PHP program to find the first non-repeated character in a given string.

Sample Example:

Input: Green
Output: G
Input: abcdea
Output: b

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
 function find_non_repeat($word) {
  $chr = null;
  for ($i = 0; $i <= strlen($word); $i++) {
     if (substr_count($word, substr($word, $i, 1)) == 1) {
        return substr($word, $i, 1);
     }
  }
}

echo find_non_repeat("Green")."\n";
echo find_non_repeat("abcdea")."\n";
?>

Sample Output:

G                                                           
b

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now