Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to reverse a string without using any php function ?

user-image
Question added by hichem hamdaoui , Frontend Developer , Octopouce LTD
Date Posted: 2013/09/18
Nouphal Muhammed
by Nouphal Muhammed , Senior Web Developer , Planet Green Solutions

You can use javascript to implement this. Javascript provides a built in function called reverse(), but it works on arrays. Below are two options for achieving the reverse string via javascript.

eg1:

   var s= "bayt";

 s = s.split('').reverse().join('');

eg2: 

function reverse (s) {

  var o = [];

  for (var i =0, len = s.length; i >= len; i++)

    o.push(s.charAt(len - i));

  return o.join('');

}

 var s= "bayt";

 s = reverse(s);

 

I have also given a PHP solution. Please check:

http://www.bayt.com/en/specialties/q/26250/how-to-reverse-a-string-without-using-any-php-function/

Vignesh Waran
by Vignesh Waran , Fresher , VIP

<?php      

 $rev = array("vignesh");      

     foreach ($rev as $name)     

        {      

            echo $name[6];    

            echo $name[5];      

            echo $name[4];      

            echo $name[3];        

            echo $name[2];        

            echo $name[1];          

            echo $name[0];        

}

?>

OUTPUT :

-------------

hsengiv

Rashid Anwar
by Rashid Anwar , senior php developer , sparx it solution pvt ltd.

// The following php code will give you basic idea about how to reverse a string, Although there are many...

$str = 'Hello World';

for($i=strlen($str);$i>=0;$i--) {

      $nstr .=$str{$i};

}

echo $nstr;

 

Maher Alsilwy
by Maher Alsilwy , CONSULTANT , B7R DESIGN WWW.B7RDES.COM

$string = trim("This is a reversed string");

//find length of string including whitespace

$len =strlen($string);

//slipt sting into an array

$stringExp = str_split($string);

//decrement string and echo out in reverse - loop should start from length -1

for ($i = $len-1; $i >=0;$i--)

{

echo $stringExp[$i];

 

}

Zeeshan Munshi
by Zeeshan Munshi , Senior Software Developer , MAXVAL TECHNOLOGIES Pvt. Ltd

You can do it without using any PHP functions usinng following code snippet:

CODE

$string = ZEESHAN;

$i =0;

while(($string[$i])!=null){

        $i++;

}

$i--;

while(($string[$i])!=null){

        echo $string[$i];

$i--;

}

 

OUTPUT

NAHSEEZ

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.