0

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>

Edited by Hitman Mania: n/a

4
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by Hitman Mania
0
$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.

Edited by wrivera: mistake

1

@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

Edited by nav33n: n/a

2

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"].')';
Votes + Comments
Neat! :)
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.