Question

In: Computer Science

Write a PHP script that checks a word or a phrase (stored in a string variable)...

Write a PHP script that checks a word or a phrase (stored in a string variable) to determine if it is a standard palindrome, a perfect palindrome, or not a palindrome at all. Also, for each letter that the word/phrase contains, count and print the number of times that each consonant and vowel is encountered. Your output will look as follows:

Word/phrase: racecar

Perfect palindrome

Contains consonants:

r - 2

c - 2

Contains vowels:

a - 2

e - 1

Solutions

Expert Solution

<?php
$word = "racecar";
echo "Word/phrase: $word<br>";
$reversed_word = strrev($word);
// converting to lower character after replacing special char
$refined_word = strtolower(preg_replace("([^A-Za-z0-9])", "", $word ));
$reversed = strtolower(strrev($refined_word));

if ($word === $reversed_word)
{
   echo "Perfect Palindrome<br>";
}
else{
   if ($refined_word == $reversed){
       echo "Standard Palindrome<br>";
   }
   else{
       echo "Not a palindrome<br>";
   }
}

$vowel = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');

echo "Contains consonants:<br>";
foreach (count_chars($word, 1) as $strr => $value) {
   if (ctype_alpha(chr($strr)) &&(! in_array(chr($strr), $vowel))) {
       echo chr($strr) . " - $value <br>";
}
}

echo "Contains vowels:<br>";
foreach (count_chars($word, 1) as $strr => $value) {
   if (ctype_alpha(chr($strr)) &&(in_array(chr($strr), $vowel))) {
       echo chr($strr) . " - $value <br>";
}
}


Related Solutions

Write a PHP program that checks the elements of a string array named $Passwords. Use regular...
Write a PHP program that checks the elements of a string array named $Passwords. Use regular expressions to test whether each element is a strong password. For this exercise, a strong password must have at least one number, one lowercase letter, one uppercase letter, no spaces, and at least one character that is not a letter or number. (Hint: Use the [^0-9A-Za-z] character class.) The string should also be between 8 and 16 characters long. The $Passwords array should contain...
Write a script that will first initialize a string variable that will store x and y...
Write a script that will first initialize a string variable that will store x and y coordinates of a point in the form ‘x 3.1 y 6.4’. Then, use string manipulating functions to extract the coordinates and plot them. ANS % create a string variable of the form 'x 3.1 y 6.4' % extract the x and y coordinates and plot str = 'x 2.3 y 4.5'; [letter rest] = strtok(str); [x rest] = strtok(rest); [letter rest] = strtok(rest); y...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
a) Given a variable word that has been assigned a string value,write a string expression...
a) Given a variable word that has been assigned a string value, write a string expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the string "(sadly)"b) Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents" on a line by itself. So,...
Linux Have your script request a word from the user, then checks if it is an...
Linux Have your script request a word from the user, then checks if it is an ordinary file. If yes display the size of the file. If the name is a directory, then display the number of objects in that directory, then display the second line of a long listing of that directory.
Write a simple PHP script. In your script use all of the following: HTML, Javascript, and...
Write a simple PHP script. In your script use all of the following: HTML, Javascript, and PHP.
IN PYTHON Write a function capitalize_last(phrase) that takes in a string phrase consisting of arbitrarily capitalized...
IN PYTHON Write a function capitalize_last(phrase) that takes in a string phrase consisting of arbitrarily capitalized words separated by spaces, and returns a new string consisting of those words but with the only the last letter in each word uppercase. You can assume that the string contains only letters and spaces, no punctuation. Examples: >>> capitalize_last('tEst WiTH rANdoM capITaliZATioN') 'tesT witH randoM capitalizatioN' >>> capitalize_last('') '' >>> capitalize_last('i am the senate') 'I aM thE senatE'
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Given an int variable k, write an expression whose value is the k-th character in the String variable word.
1. Given an int variable k, write an expression whose value is the k-th character in the String variable word. Assume thatword has been initialized and that the value ofk is non-negative and less than the length ofword.Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'i'.Example 2: if k's value is 0 andword is "fall", the value of the expression would be 'f'.2. Given an int variable k, write an expression whose...
Write a PHP script to display in frequency all the words in the submitted paragraph. Convert...
Write a PHP script to display in frequency all the words in the submitted paragraph. Convert the string to an array of words. Count the number of times the words are used. Hint: array_count_values($arrayname) Sort the array in frequency order. Print the array to the screen. Hint: print_r($arrayname)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT