So I'm trying to make a program that counts how many vowels are there in the user's first name. I tried to make the code but its not 100% functional, can someone take a look and tell me where I went wrong?

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
Enter your First Name:<br><br>
<form action="test2.php" method="post">
Name: <input type="text" name="fname" />
<input type="submit" />
</form>
<?php
$vowels=array("a","e","i","o","u"); 
$length=strlen($_POST["fname"]); 
$count = 0; 
for ($i = 0; $i != $length; $i++) 
{ 
	if (array_search($_POST["fname"][$i], $vowels)) 
	{ 
		$count++; 
	} 
} 
echo 'There are (' . $count . ') vowels in the string (' . $_POST["fname"] . ').'; 
?>
</body>
</html>

Recommended Answers

All 5 Replies

$vowels=array("a","e","i","o","u");
$length=strlen($_POST["fname"]);

$count = 0;
for ($i = 0; $i < $vowels; $i++){
  for($j=0; $j<$length; $j++){
    $char=substr($_POST["fname"],$j);
   if ($char==$vowels[$i]){
     $count++;
  }
 }
}
echo 'There are (' . $count . ') vowels in the string (' . $_POST["fname"] . ').';

you need to search through each character and each array element so you need to set up to for loops in order to accomplish this.

Also: your use of array search and your for loop are incorrect.

@Hitman Mania, The problem with your code is, the function array_search will return the key if it finds the needle in the array. Since "a" is the first element of the array, this will return 0 [the key].
0 which also corresponds to "false" is causing your script to fail.

@wrivera, Why use 2 loops when you can use one ? ie.,

<?php
if(isset($_POST['submit'])) {
	$vowels = array("a","e","i","o","u");
	$username = $_POST['fname'];
	$length = strlen($username);
	$count = 0;
	for($i=0;$i< $length; $i++) {
		$count = (in_array(strtolower($username{$i}),$vowels)) ? ($count + 1 ) : ($count + 0);
	}
	echo "There are {$count} vowels in $username<br />";
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
Enter your First Name:<br><br>
<form action="test2.php" method="post">
Name: <input type="text" name="fname" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

in_array checks if a character is in the vowel array. If yes, increment count, else, don't do anything.

strtolower will make sure that it counts the vowels even if the user has entered his username in uppercase.

Cheers!
Naveen

A simpler way would be this single-line solution:

print 'There are '.preg_match_all('/[aeiou]/i',$_POST["fname"],$matches).' in the string ('.$_POST["fname"].')';
commented: Neat! :) +6

A simpler way would be this single-line solution:

print 'There are '.preg_match_all('/[aeiou]/i',$_POST["fname"],$matches).' in the string ('.$_POST["fname"].')';

very true much more efficient.

Alright guys this was very helpful, thank you all.

Hitman :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.