How to reverse given string without using built in functions?

Showing Answers 1 - 45 of 45 Answers

chatthan

  • Apr 4th, 2007
 

print the charcters of the string in reverse order... using a for loop.. from 0 to length of string .. and print in reverve order.. thats all

Manish Painuly`

  • Jun 28th, 2007
 

with function:

WE CAN USE strrev() function


without function

for($i=0;sizeof($str_name)>$i;$i++)
{

for($l=1;$i-$l;$l--)
{
//print string here
}

  Was this answer useful?  Yes

neeraj

  • Sep 14th, 2007
 


 $len = strlen($str_name);
for($i=($len-1); $i>=0;$i--)
  {
   echo $str_name[$i];
  }

<?php
    $st = "ARUMUGAM";
    $len = 1;
    $length = 0;
    for($i=0; $i<$len; $i++){
        if(isset($st[$i])){
            //print $st[$i];
            //print "<br/>";
            $length = $i;
        }
        if($length != $i){
            break;
        }
        if($i == ($len-1)){
            $len = $len + 1;
        }
    }
   
    //print $length;
    //print "<br/>";
    for($i=$length; $i>=0; $i--){
        print $st[$i];
    }
?>

amitdubey2

  • Mar 29th, 2010
 

<?php
$str_name='Sachin Tendulkar';
$len=strlen($str_name);
//echo $len;
$i=$len;
for ($i;$i>-1;$i--)   
//$i>-1 because in array Ist element is start with 0th position.
{
echo $str_name[$i];
}
?>

Ravindra Singh

  • Jan 29th, 2013
 

Code
  1. $string = RAVINDRA;

  2. $i = 0;

  3. while(isset($string[$i])){

  4.         $i++;

  5. }

  6. $i--;

  7. while(isset($string[$i])){

  8.         echo $string[$i];

  9. $i--;

  10. }

  11.  

Prerna

  • Nov 11th, 2014
 

Code
  1.  $str="PRERNA";

  2.  $c= strlen($str)-1;

  3.  

  4.  

  5. while(isset($str[$c]))

  6. {

  7.     echo $str[$c];

  8.     $c--;

  9.    

  10. }

  Was this answer useful?  Yes

This method does only half of the iterations

Code
  1. function reverse($str)

  2. {

  3.     $start = 0;

  4.     $end = strlen($str) - 1;

  5.  

  6.     while ($start < $end) {

  7.         $tmp = $str[$start];

  8.         $str[$start] = $str[$end];

  9.         $str[$end] = $tmp;

  10.  

  11.         $start++;

  12.         $end--;

  13.     }

  14.  

  15.     return $str;

  16. }

  Was this answer useful?  Yes

rajesh

  • Jun 2nd, 2015
 

Code
  1. $string="rajesh";

  2. $str=strlen($string);

  3. for($i=($str-1);$i<=$str;i--);

  4. {

  5. echo $string[$i];

  6. }

  Was this answer useful?  Yes

Recursive answer:

Code
  1. function reverseString($string) {

  2.         if (strlen($string) === 1) {

  3.                 return $string;

  4.         }

  5.        

  6.         return substr($string,-1) . reverseString(substr($string, 0, -1));     

  7. }

  Was this answer useful?  Yes

Vishnu R menon

  • Dec 28th, 2015
 

$val="vishnu menon is a good boy";
echo(implode(array_reverse(str_split($val))));

  Was this answer useful?  Yes

Dharmendra Singh

  • Feb 3rd, 2016
 

Copy below code and run on your host

Code
  1.  

  2. <?php

  3. ini_set("display_errors", "0");

  4. $string = $_POST[text];

  5. $length = 0;

  6. for($d=0;$d<10;$d++){

  7.         if($string[$d] != ""){

  8.                 $length++;

  9.                 }else{

  10.                         break;

  11.                 }

  12. }

  13. ?>

  14. <div style="margin:30vh auto; text-align:center;">

  15.   <form name="form1" method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>" >

  16.     <input type="text" name="text" />

  17.     <input type="submit" />

  18.   </form>

  19.   <br />

  20.   <br />

  21.   <br />

  22.   <p>

  23.     <?php for($a=$length-1;$a>=0;$a--){

  24.         echo $string[$a];

  25. }

  26. ?>

  27.   </p>

  28. </div>

  29. </body>

  30. </html>

  Was this answer useful?  Yes

manish

  • Jul 25th, 2016
 

Reverse String Program

Code
  1.  

  2. <?php

  3. // reverSe String Program

  4. $a="Manish kumar 123";

  5.  

  6. $b=strlen($a);

  7. for($i=($b-1);$i>=$a;$i--)

  8. {

  9. $c[]=$a[$i];

  10.        

  11.         }

  12.         echo implode($c,);

  13.  

  14. ?>

  15.  

  Was this answer useful?  Yes

Ankit

  • May 25th, 2017
 

Reverse String Program

Code
  1.  $string = "ABCDE";

  2.     $i = 0;

  3.     while(isset($string[$i])){

  4.         $i++;  

  5.     }

  6.     $i--;

  7.     for($j = $i; $j >= 0; $j--){

  8.         echo $string[$j];

  9.     }

  Was this answer useful?  Yes

bibin

  • Jan 12th, 2018
 

Function reversestring($string) {
$len = strlen($string);
for($i = $len-1; $i>=0; $i--) {
echo $string[$i];
}
}
$revstr = reversestring(COMPUTER);

Code
  1. function reversestring($string) {

  2.         $len = strlen($string);

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

  4.                 echo $string[$i];

  5.         }

  6. }

  7. $revstr = reversestring(COMPUTER);

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions