STRING HANDLING
<?php
// Function to count the number of words in a string
function countWords($str) {
$words = str_word_count($str);
return $words;
}
// Function to reverse a string
function reverseString($str) {
$reversedStr = strrev($str);
return $reversedStr;
}
// Function to convert a string to uppercase
function convertToUppercase($str) {
$uppercaseStr = strtoupper($str);
return $uppercaseStr;
}
// Function to convert a string to lowercase
function convertToLowercase($str) {
$lowercaseStr = strtolower($str);
return $lowercaseStr;
}
// Sample string
$string = "Hello World! This is a PHP program for PG students.";
// Count words in the string
$wordCount = countWords($string);
echo "Number of words in the string: $wordCount<br>";
// Reverse the string
$reversedString = reverseString($string);
echo "Reversed string: $reversedString<br>";
// Convert the string to uppercase
$uppercaseString = convertToUppercase($string);
echo "Uppercase string: $uppercaseString<br>";
// Convert the string to lowercase
$lowercaseString = convertToLowercase($string);
echo "Lowercase string: $lowercaseString<br>";
?>