Notification texts go here Contact Us Buy Now!

PHP - reverseWords from String - Solution

Aprodesoft

We need to write code in PHP where the Input will be passed to us in String and we have to reverse the order of the words.

Example:
Input- I am Developer
Output- Developer am I

Problem Statement:
Write a function reverseWords() that takes a message as a string and reverses the order of the words in place

A solution in PHP:

function reverseWords($message){    return implode(' ', array_reverse( str_word_count( $message, 1)));}

In the above solution, we have taken simple inbuilt functions of PHP.

str_word_count – It returns information about words used in a string, when using with the second param as 1 we will get an array containing all the words found inside the input string.

array_reverse – It returns an array with elements in reverse order

implode – It joins array elements with a string 

Keep Coding and Keep Smiling 🙂 

About the Author

Aprodesoft
saya seorang yang bisa diadalakan dalam mencari rekomendasi atau informasi yang bagus untuk anda.

Posting Komentar

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.