Reputation: 339
In yesterday Interview I was asked that how to reverse a string with out using any string function like strrev() or strlen(). I found this example on website but it gives error.is it possible to do this without strlen().
Uninitialized string offset: -1 in D:\xampp\htdocs\PhpProject1\index.php on line xx
$string = "ZEESHAN";
$i =0;
while(($string[$i])!=null){
$i++;
}
$i--;
while(($string[$i])!=null){
echo $string[$i];
$i--;
}
Upvotes: 4
Views: 43289
Reputation: 349
With a minor modifications
<?php
$string = "Hello";
$reverse = "";
$i = 0;
while($i < strlen($string)){
$reverse = $string[$i].$reverse;
$i++;
}
echo $reverse;
?>
Upvotes: 0
Reputation: 1
Check this code to resolve this problem.
<?php
$string = "ZEESHAN";
$i =0;
while(!empty($string[$i])){
$i++;
}
$i--;
while(!empty($string[$i])){
echo $string[$i];
$i--;
}
?>
Upvotes: 0
Reputation: 1
<?php
$string = 'ZEESHAN';
$reverse = '';
$i = 0;
while(!empty($string[$i])){
$reverse = $string[$i].$reverse;
$i++;
}
echo $reverse;
?>
Check complete post: https://www.troposal.com/reverse-string-without-function-php/
Upvotes: -1
Reputation: 71
//Program for reversing a string
<?php
class str_rev{
public function Revese_Str($string){
$reverse = '';
$i=0;
while(!empty($string[$i])){
$reverse = $string[$i].$reverse;
$i++;
}
return $reverse;
}
}
$object = new str_rev();
echo $object->Revese_Str('Ayush Jain');
Upvotes: 0
Reputation: 344
I have created a simple logic for string reversal
$str = 'ABCD';
$strReversed = '';
$length = strlen($str);
for($i=$length-1; $i >= 0; $i--){
$strReversed .= $str[$i];
}
echo $strReversed;
Upvotes: 1
Reputation:
Please take a look the below code: Reverse the string
$str = 'abcdefg';
$reverseString = '';
for($i=strlen($str);$i<=strlen($str);$i--){
$reverseString .= $str[$i];
if($i==0)
break;
}
echo $reverseString;
The above code output is:
gfedcba
Upvotes: -1
Reputation: 1
<!doctype html>
<html>
<body>
<center>
<form action="#" method="get">
<br>
<input type="text" name="txt"/>
<br><br>
<input type="submit" name="submit"/>
<br><br>
</form>
<?php
if(isset($_GET["submit"])) {
$name = $_GET["txt"];
for ($i = 0; isset($name[$i]); $i++) {
$j = $i;
}
for ($k = $j; isset($name[$k]); $k--) {
echo $name[$k];
}
}
Upvotes: -1
Reputation: 1244
You can try it with count_chars
and array_sum
<?php
$string = "ZEESHAN";
$count=array_sum(count_chars($string));
for($i=$count -1 ;$i>=0;$i--){
echo $string[$i];
}
?>
If you dont want any php string function you can try this way with krsort
and array
:
<?php
$string = "ZEESHAN";
$arr = array();
$i=0;
while(isset($string[$i])){
$arr[$i] = $string[$i];
$i++;
}
krsort($arr);
$final = implode("",$arr);
var_dump($final);
?>
Upvotes: 0
Reputation: 31749
Try -
$string = "ZEESHAN";
$j = 0;
while(!empty($string[$j])) {
$j++;
}
for($i = ($j-1); $i >= 0; $i--) {
echo $string[$i];
}
Output
NAHSEEZ
Upvotes: 1
Reputation: 1897
You can do this
$string = "heya";
$i = strlen("heya") - 1;
$newStr = "";
while($i >= 0) {
$newStr .= $string[$i];
$i--;
}
echo $newStr; // output: ayeh
Upvotes: -1
Reputation: 21437
You can use while
and for
loop like as
$string = "ZEESHAN";
$reverse = "";
$j = 0;
while(isset($string[$j])){
$j++;
}
for($i = $j - 1; $i >= 0; $i--){
$reverse .= $string[$i];
}
echo $reverse;
Upvotes: 0
Reputation: 711
$string = 'zeeshan';
$reverse = '';
$i = 0;
while(!empty($string[$i])){
$reverse = $string[$i].$reverse;
$i++;
}
echo $reverse;
Upvotes: 17