In: Computer Science
When writing code in any language, it is always a good idea to work in a modular fashion. In this approach you will write a small piece of code (usually a small logical module), and test to ensure it works. If it does, you will add more code, and test it again. This way, if the program were to crash after you last addition, you will know where to look for bugs. Debugging a few lines of code is much easier than typing pages of code and then trying to debug that. Please use this approach while working at your programming assignments.
In this assignment you will write a small program that demonstrates that you can manipulate a string. You can hardcode a string of your choice in your code, that is, create a variable and assign it a string "Mary had a little chicken". Then you will use string functions to manipulate it. Keep in mind that to test your program I will enter my own string into your code. So, your program has to work for all possible strings and produce correct output.
First, echo the string on the screen, and then print it
backwards using a string function. Figure out how to count the
total number of letters in the string using functions provided in
the chapter, and the number of vowels and consonants. For this
exercise, please treat letter "y" as a vowel. Your output should
look exactly like this:
Original string: Mary had a little
chicken
Reversed string: nekcihc elttil a dah yraM
Total letters: 21
Vowels: 8
Consonants: 13
Save your script as mystringyourlastname.php and submit to BB via submission portal before the deadline. Be sure to thoroughly test your code on xampp server with Internet Explorer and/or Firefox. I will be using xampp to test your submission. Test your assignment with various input strings to ensure it operates correctly. I will check different strings to make sure you have done error checking.
Please plan to have enough time for testing and debugging. I will nor be debugging your code for you. Please keep in mind that I ALWAYS read you code, so do not forget to format it nicely and use comments, as points will be deducted for such violations. Program that does not work will receive ZERO credit, as it is pointless to produce a program that does not work. Partial credit for depend on the existing functionality, per rubric below.
PHP CODE
<?php
$inputString = "Mary had a little chicken";
echo "Original string: ", $inputString;//Displaying string
echo "<br>";
echo "Reversed string: ", strrev($inputString);//String is reversed and displayed
$vowelCount = 0;
$constCount = 0;
$inputString = strtolower($inputString); //Converting all characters in string into lowercase
for($i = 0; $i < strlen($inputString); $i++)
{ //Check whether it is vowel letter or 'y'
if( $inputString[$i] == 'a' || $inputString[$i] == 'e' || $inputString[$i] == 'i' || $inputString[$i] == 'o' || $inputString[$i] == 'u' || $inputString[$i] == 'y')
{
$vowelCount++;//Increments vowel count
}
//Check consonants
else if($inputString[$i] >= 'a' && $inputString[$i] <= 'z')
{
$constCount++; //Increments consonant count
}
}
echo "<br>";
echo "Total letters: ";
echo $vowelCount + $constCount;//display total character count
echo "<br>";
echo "Vowels: ";
echo $vowelCount;//display vowel count
echo "<br>";
echo "Consonants: ";
echo $constCount;//dispaly consonant count
?>
PHP CODE SCREENSHOT
; ech"
src=
"https://media.cheggcdn.com/coop/b69/b698238a-caa6-4d80-b176-730890bd0646/1603466008057_2020-10-2319.png"
style="height:766px;" />OUTPUT SCREENSHOT