Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:32:57+00:00 2026-05-15T02:32:57+00:00

This task has already been asked/answered, but I recently had a job interview that

  • 0

This task has already been asked/answered, but I recently had a job interview that imposed some additional challenges to demonstrate my ability to manipulate strings.

Problem: How to reverse words in a string? You can use strpos(), strlen() and substr(), but not other very useful functions such as explode(), strrev(), etc.

Example:

$string = "I am a boy"

Answer:

I ma a yob

Below is my working coding attempt that took me 2 days [sigh], but there must be a more elegant and concise solution.

Intention:

1. get number of words
2. based on word count, grab each word and store into array
3. loop through array and output each word in reverse order

Code:

$str = "I am a boy";

echo reverse_word($str) . "\n";

function reverse_word($input) {
    //first find how many words in the string based on whitespace
    $num_ws = 0;
    $p = 0;
    while(strpos($input, " ", $p) !== false) {
        $num_ws ++;
        $p = strpos($input, ' ', $p) + 1;
    }
    
    echo "num ws is $num_ws\n";
    
    //now start grabbing word and store into array
    $p = 0;
    for($i=0; $i<$num_ws + 1; $i++) {
        $ws_index = strpos($input, " ", $p);
        //if no more ws, grab the rest
        if($ws_index === false) {
            $word = substr($input, $p);
        }
        else {
            $length = $ws_index - $p;
            $word = substr($input, $p, $length);
        }
        $result[] = $word;
        $p = $ws_index + 1; //move onto first char of next word
    }
    
    print_r($result);
    //append reversed words
    $str = '';
    for($i=0; $i<count($result); $i++) {
        $str .= reverse($result[$i]) . " ";
    }
    return $str;
}

function reverse($str) {
    $a = 0;
    $b = strlen($str)-1;
    while($a < $b) {
        swap($str, $a, $b);
        $a ++;
        $b --;
    }
    return $str;
}

function swap(&$str, $i1, $i2) {
    $tmp = $str[$i1];
    $str[$i1] = $str[$i2];
    $str[$i2] = $tmp;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:32:57+00:00Added an answer on May 15, 2026 at 2:32 am
    $string = "I am a boy";
    
    $reversed = "";
    $tmp = "";
    for($i = 0; $i < strlen($string); $i++) {
        if($string[$i] == " ") {
            $reversed .= $tmp . " ";
            $tmp = "";
            continue;
        }
        $tmp = $string[$i] . $tmp;    
    }
    $reversed .= $tmp;
    
    print $reversed . PHP_EOL;
    >> I ma a yob
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I the next task that has not already been done by
This one has me puzzled. It seemed like an easy task, but the solution
I have an old .net 2005 web site that has some asp pages and
This little asp.net code has some drawbacks. If the itemrow is empty, it will
I have a list of string that have already been arranged according to a
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I was once given this task to do in an RDBMS: Given tables customer,
For customer service week this year, I have the privileged task of creating a
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.