Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to reverse a string without using any php function ?

user-image
Question ajoutée par hichem hamdaoui , Frontend Developer , Octopouce LTD
Date de publication: 2013/09/18
Nouphal Muhammed
par 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
par 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
par 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
par 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
par 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