w3resource

PHP String Exercises : Change the case of letters or words

PHP String: Exercise-1 with Solution

Write a PHP script to : -

a) transform a string all uppercase letters.
b) transform a string all lowercase letters.
c) make a string's first character uppercase.
d) make a string's first character of all the words uppercase.

Visual Presentation:

PHP String Exercises: Change the case of letters or words

Sample Solution:

PHP Code:

<?php
// Convert all characters to uppercase
print(strtoupper("the quick brown fox jumps over the lazy dog."))."\n";
// Convert all characters to lowercase
print(strtolower("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"))."\n";
// Make the first character uppercase
print(ucfirst("the quick brown fox jumps over the lazy dog."))."\n";
// Make the first character of each word uppercase
print(ucwords("the quick brown fox jumps over the lazy dog."))."\n";
?>

Output:

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.                
the quick brown fox jumps over the lazy dog                 
The quick brown fox jumps over the lazy dog.                
The Quick Brown Fox Jumps Over The Lazy Dog.

Explanation:

In the exercise above,

  • print(strtoupper("the quick brown fox jumps over the lazy dog.")): Converts all characters in the string to uppercase.
  • print(strtolower("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")): Converts all characters in the string to lowercase.
  • print(ucfirst("the quick brown fox jumps over the lazy dog.")): Makes the first character of the string uppercase.
  • print(ucwords("the quick brown fox jumps over the lazy dog.")): Makes the first character of each word in the string uppercase.

Flowchart :

Flowchart: Change the case of letters or words

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: PHP String Exercises Home.
Next: Write a PHP script to split the specific string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-string-exercise-1.php