喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
     
标题: [讨论] 无字符串和数组处理函数的字符串反转.  
 
圣斗士的信仰
注册会员
Rank: 2



UID 84775
精华 0
积分 74
帖子 76
金钱 74 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-12-13
状态 离线
[广告]: q m
无字符串和数组处理函数的字符串反转.

//'This is a book' 反转后->'book a is This'

<?php
$foo = 'This is a book';
$i = 0;
$result = '';
$temp = '';
while(isset($foo{$i}))
{
    if ($foo{$i} === ' ')
    {
        $result = ' ' . $temp . $result;
        $temp = '';
    }
    else
    {
        $temp .= $foo{$i};
    }
    $i ++;
}
$result = $temp . $result;
echo $result;
?>


<?php
$str = 'quick fox jumps over lazy dog';
$res = '';
$exp = array();

function my_count($str)
{
    $i = 0;

    while (isset($str[$i])) {
        ++$i;
    }

    return $i;
}

for ($i = 0, $j = 0, $max = my_count($str); $i < $max; ++$i) {
    if ($str[$i] == ' ') {
        ++$j;
        continue;
    }

    $exp[$j] .= $str[$i];
}

for ($i = my_count($exp) - 1; $i >= 0; --$i) {
    if ($i == 0) {
        $res .= "{$exp[$i]}";
    } else {
        $res .= "{$exp[$i]} ";
    }
}

var_dump($res);
?>

<?php
$str="This is a book";
$result='';
$chars = preg_split('/ /', $str);
foreach($chars as $value)
{
    $result = $value . ' ' . $result;
}
print $result;
?>

<?
$str = 'quick fox jumps over lazy dog';
$res = '';
$tmp = '';

$i = 0;
while (($str[$i] || $str[$i-1]) && $tmp .= $str[$i++]) {
    if ($str[$i-1] == ' ' || $str[$i-1] == NULL) {
        $res = $str[$i-1] == NULL ? "$tmp $res" : "$tmp$res";
        $tmp = '';
    }
}

echo $res;
?>

<?php
$str = 'quick fox jumps over lazy dog';
$res = '';
$tmp = '';

$i = $first = 0;
while ((isset($str[$i]) || isset($str[$i-1])) &&
       ($tmp = (isset($str[$i]) && $str[$i] == ' ') ? $tmp : "{$tmp}{$str[$i]}")) {
    ++$i;
    if ((isset($str[$i-1]) && $str[$i-1] == ' ') || !isset($str[$i-1])) {
        ($first == 0) ? (($first = 1) && $res = $tmp) : $res = "$tmp $res";
        $tmp = '';
    }
}

var_dump($res);
?>

<?
$str = 'quick fox jumps over lazy doggie';
$res = $str;
while ($str[$len++]);
for ($i = $len-1, $j = 0; $i >= 0; --$i) {
    if (($str[$i]==' ' and $k=$i+1) || ($i==0 and !($k=$i))) {
        while ((isset($str[$k]) && $str[$k] != ' ') && $res[$j++] = $str[$k++]);
        $j+1 <= $len-1 and $res[$j++] = ' ';
    }
}

var_dump($res);
?>

<?php
$foo = 'This is a book';
function bar($foo)
{
$i = 0;
$result = '';
$temp = '';
while(isset($foo{$i}))
{
    if ($foo{$i} === ' ')
    {
        $result = ' ' . $temp . $result;
        $temp = '';
    }
    else
    {
        $temp .= $foo{$i};
    }
    $i ++;
}
$result = $temp . $result;
return $result;
}
echo bar($foo);
?>





<?
$s='this is a book';
for($i=0,$m=$n='';isset($s[$i]);$i++)
{
if($s[$i]==" "){$m=$n.' '.$m;$n='';}else{$n.=$s[$i];}
}
echo $n.' '.$m;
?>

<?php
$result = '';
$return = '';
$a='This is a book';
$i = 0;

function turn( $str, $length= 0 )
{
        global $result, $return;
        $tmp = '';
        $i = $length;

        for(;; $i++ )
        {
                if ( isset($str{$i}) )
                        if ($str{$i} == " " )
                        {
                                $result = $tmp . ' ' . $result;
                                turn( $str, $i + 1 );
                        }
                        else
                                $tmp .= $str{$i};
                else
                {
                        if ( $return == '' )
                                $return = $tmp . ' ' . $result;
                        else
                                break;
                }
        }
}

turn( $a );

echo $return;
?>

<?php
function turn1( $str )
{
        $return = '';
        $tmp    = '';

        for(;; $i++ )
        {
                if ( isset( $str{$i} ) )
                {
                        $tmp .= $str{$i};

                        if ( $str{$i} == " " )
                        {
                                $result = $tmp . $result;
                                $tmp = '';
                        }
                }
                else
                        return $tmp . ' ' . $result;
        }
}

echo turn1('This is a book');
?>

<?php
$str = 'This is a book';

$i = 0;

function fun($i)
{
    global $str;
    $k = $i;
   
    for(;; $k-- )
    {
        if(!isset($str[$k]) || $str[$k]==' ')
        {
            for($j = $k+1;$j <= $i; $j++ )print $str[$j];

            if(!isset($str[$k]))return;
            else
            {
                print " ";
                return fun($k-1);  
            }
        }
    }

}

while (isset($str[$i])) {
    $i++;
}
fun($i-1);
?>

<?
$string = 'This is a book';
$string = $string . ' ';
$i = 0;
while( isset( $string[$i] ) )
{
    if( $string[$i] == ' ' )
    {
        $word = $s . ' ' . $word;
        $s = '';
    }else
    {
        $s .= $string[$i];
    }
    $i++;
}
echo $word;

?>
2007-9-12 02:19 PM#1
查看资料  发短消息  顶部
 
xjxaixxy (xjxaixxy)
新手上路
Rank: 1
初级会员



UID 31002
精华 0
积分 15
帖子 19
金钱 15 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2004-9-9
状态 离线
[推荐阅读] xajax晕晕晕,新手搞不定啊
???不是有反转函数么
2007-9-14 11:27 AM#2
查看资料  发短消息  顶部
 
圣斗士的信仰
注册会员
Rank: 2



UID 84775
精华 0
积分 74
帖子 76
金钱 74 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-12-13
状态 离线
[推荐阅读] 求算法,多维数组和一维数组的交互运算问题!
是个面试题,不让用任何字符串和数组处理函数的。
2007-9-14 11:36 AM#3
查看资料  发短消息  顶部
 
guo664
金牌会员
Rank: 6Rank: 6


UID 74604
精华 0
积分 2390
帖子 301
金钱 2390 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2006-6-7
状态 离线
[推荐阅读] 在discuz6.0中 common.inc.php文件中碰到一个问题,帮忙解答,谢谢
$str="cctv";
$i=0;
while ( isset($str[$i])){
        $string=$str[$i].$string;
$i++;
}
echo $string;
2007-9-14 12:31 PM#4
查看资料  Blog  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.029806 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP