2022-09-20

Sep 21 In-Class Exercise Thread.

Please post your solutions to the Sep 21 In-Class Exercise to this thread.
Best,
Chris
(Edited: 2022-09-22)
Please post your solutions to the Sep 21 In-Class Exercise to this thread. Best, Chris

-- Sep 21 In-Class Exercise Thread
function my_cool_function($sentence) { $words = explode(" ", $sentence); end($words); while(current($words)) { echo ucfirst(current($words)), " "; prev($words); } echo "\n"; }
(Edited: 2022-09-21)
<nowiki> function my_cool_function($sentence) { $words = explode(" ", $sentence); end($words); while(current($words)) { echo ucfirst(current($words)), " "; prev($words); } echo "\n"; } </nowiki>

-- Sep 21 In-Class Exercise Thread
    function revStr($str)
    {
        if (strlen($str) == 0) {
            print "Invalid string";
            return;
        }
        $arr = explode(" ", $str);
        for ($x = count($arr)-1; $x >= 0; $x--) {
            print strtoupper($arr[$x][0]).substr($arr[$x], 1)." ";
        }
    }
function revStr($str) { if (strlen($str) == 0) { print "Invalid string"; return; } $arr = explode(" ", $str); for ($x = count($arr)-1; $x >= 0; $x--) { print strtoupper($arr[$x][0]).substr($arr[$x], 1)." "; } }

-- Sep 21 In-Class Exercise Thread
 <?php
 $string = "the quick brown fox";
 function reverse($str)
 {
 	$arr = explode(" ", $str);
 	krsort($arr);
 	foreach($arr as $key => $value){
 		$arr[$key] = ucfirst($value);
 	}
 	$str = implode(" ", $arr);
 	echo $str;
 }
 reverse($string);
(Edited: 2022-09-21)
<?php $string = "the quick brown fox"; function reverse($str) { $arr = explode(" ", $str); krsort($arr); foreach($arr as $key => $value){ $arr[$key] = ucfirst($value); } $str = implode(" ", $arr); echo $str; } reverse($string);

-- Sep 21 In-Class Exercise Thread
function reverseStrCaps($txt){
	$arr = explode(" ", $txt);
	$arr_r = array_reverse($arr);
	$txt_r = join($arr_r, " ");
	echo ucwords($txt_r);
}
function reverseStrCaps($txt){ $arr = explode(" ", $txt); $arr_r = array_reverse($arr); $txt_r = join($arr_r, " "); echo ucwords($txt_r); }

-- Sep 21 In-Class Exercise Thread
function capitalizeFirstAndreverse(){
          $a = ["the", "quick", "brown", "fox"];
          foreach ($a as $value) {
            ucfirst($value);
          };
          krsort($a);
          foreach ($a as $string) {
            echo ucfirst($string);
            echo " ";
          }
}
function capitalizeFirstAndreverse(){ $a = ["the", "quick", "brown", "fox"]; foreach ($a as $value) { ucfirst($value); }; krsort($a); foreach ($a as $string) { echo ucfirst($string); echo " "; } }
2022-09-21

-- Sep 21 In-Class Exercise Thread
<nowiki><?php function reverseUpper($s) { $parts = explode(" ", $s); for($i = count($parts)-1; $i >= 0; $i--) { echo strtoupper(substr($parts[$i], 0, 1)) . substr($parts[$i], 1) . " "; } } $str = "the quick brown fox"; reverseUpper($str);</nowiki>

-- Sep 21 In-Class Exercise Thread
<?php
    $sentence = "it is wednesday my dudes";
    
    function reverseStringAndCapitalizeFirstLetter($sentence) {
        // split the string based on empty space delimiter
        $splitSentence = explode(" ", $sentence);
        // reverse through array, capitalize first letter of each word and print 
        // it out
        for ($i = count($splitSentence)-1; $i >= 0; $i--) {
            $string = $splitSentence[$i];
            $newWord = substr($string, 0, 1);
            $newWord = strtoupper($newWord);
            $newWord = $newWord . substr($string, 1, strlen($string)) . " ";
            echo $newWord;
        }
    }
    reverseStringAndCapitalizeFirstLetter($sentence);
    
?>
<?php $sentence = "it is wednesday my dudes"; function reverseStringAndCapitalizeFirstLetter($sentence) { // split the string based on empty space delimiter $splitSentence = explode(" ", $sentence); // reverse through array, capitalize first letter of each word and print // it out for ($i = count($splitSentence)-1; $i >= 0; $i--) { $string = $splitSentence[$i]; $newWord = substr($string, 0, 1); $newWord = strtoupper($newWord); $newWord = $newWord . substr($string, 1, strlen($string)) . " "; echo $newWord; } } reverseStringAndCapitalizeFirstLetter($sentence); ?>

-- Sep 21 In-Class Exercise Thread
<?php function reverseStringAndCapitalise($str) {
    $words = explode(" ", $str);
    krsort($words);
    foreach($words as $var){
        echo ucfirst($var), " ";
    }
} reverseStringAndCapitalise('the quick brown fox');
<?php function reverseStringAndCapitalise($str) { $words = explode(" ", $str); krsort($words); foreach($words as $var){ echo ucfirst($var), " "; } } reverseStringAndCapitalise('the quick brown fox');

-- Sep 21 In-Class Exercise Thread
<?php
    function reverseString($str) {
        $cur = explode(" ", $str);
        end($cur);
        while(current($cur)){
            echo strtoupper(substr(current($cur), 0, 1)), substr(current($cur), 1), " ";
            prev($cur);
        }
    }
    reverseString("the quick brown fox");
<?php function reverseString($str) { $cur = explode(" ", $str); end($cur); while(current($cur)){ echo strtoupper(substr(current($cur), 0, 1)), substr(current($cur), 1), " "; prev($cur); } } reverseString("the quick brown fox");
[ Next ]
X