Q:

PHP program to convert string to lowercase without using the library function

belongs to collection: PHP String Programs

0

Given a string and we have to convert it into lowercase string without using any library function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

PHP code:

<?php
//function definition
//this function accepts a string/text, converts
//text to lowercase and return the lowercase converted string
function lowercase($str)
{
	$chars  = str_split($str);
	$result = '';

	//loop from 0th character to the last character
	for ($i = 0; $i < count($chars); $i++) {
		//extracting the character and getting its ASCII value
		$ch = ord($chars[$i]);

		//if character is a lowercase alphabet then converting 
		//it into an lowercase alphabet
		if ($chars[$i] >= 'A' && $chars[$i] <= 'Z')
			$result .= chr($ch + 32);
		else
		$result .= $chars[$i];
	}
	//finally, returning the string
	return $result;
}

//function calling
$text = "HELLO WORLD";
echo lowercase($text);
echo "<br>";

$text = "Hello world!";
echo lowercase($text);
echo "<br>";

$text = "Hello@123.com";
echo lowercase($text);
echo "<br>";

?>

Output

hello world
hello world!
hello@123.com

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP program to convert string to lowercase without... >>
<< PHP program to convert string to uppercase without...