php字符串反转函数

Given a string and we have to reverse it without using a library function.

给定一个字符串,我们必须不使用库函数而将其反转。

Example:

例:

    Input: "Hello world!"
Output: "!dlrow olleH"
Input: "Welcome @ IncludeHelp.Com"
Output: "moC.pleHedulcnI @ emocleW"

PHP代码无需使用库函数即可反转字符串 (PHP code to reverse the string without using library function)

<?php
//PHP code to reverse the string without
//using library function
//function definition
//it accepts a string and returns the revrse string
function reverse_string($text){$rev = ''; //variable to store reverse string
$i = 0; //counting length
//calculating the length of the string
while(isset($text[$i])){$i++;
}
//accessing the element from the reverse
//and, assigning them to the $rev variable
for($j = $i - 1; $j >= 0; $j--){$rev .= $text[$j];
}
//returninig the reversed string
return $rev;
}
//main code i.e. function calling
$str = "Hello world!";
$r_str = reverse_string($str);
echo "string is: ". $str . "<br/>";
echo "reversed string is: ". $r_str . "<br/>";
$str = "Welcome @ IncludeHelp.Com";
$r_str = reverse_string($str);
echo "string is: ". $str . "<br/>";
echo "reversed string is: ". $r_str . "<br/>";
?>

Output

输出量

string is: Hello world!
reversed string is: !dlrow olleH
string is: Welcome @ IncludeHelp.Com
reversed string is: moC.pleHedulcnI @ emocleW

Explanation:

说明:

Since we can't use the library function, In the function - we run a for loop to reverse the strings by storing the sequence in reverse order in the variable $rev. An additional while loop is set up to check if the variable $text contains a valid string (i.e. to calculate the length). This is an additional safety check to ensure that the program works even if numbers are put into the function.

由于无法使用库函数,因此在函数中,我们运行一个for循环,以相反的顺序将序列存储在变量$ rev中,以反转字符串。 设置了一个附加的while循环,以检查变量$ text是否包含有效的字符串(即,计算长度)。 这是一项附加的安全检查,以确保即使在功能中输入了数字,程序也可以正常运行。

翻译自: https://www.includehelp.com/php/reverse-a-given-string-without-using-the-library-function.aspx

php字符串反转函数

php字符串反转函数_PHP | 反转给定的字符串而不使用库函数相关推荐

  1. 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个...

    2019独角兽企业重金招聘Python工程师标准>>> 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个,如"我ABC ...

  2. 编写一个截取字符串的函数,输入为一个字符串和字节数, 输出为按字节截取的字符串。 但是要保证汉字不被截半个。

    题目: 编写一个截取字符串的函数,输入为一个字符串和字节数, 输出为按字节截取的字符串. 但是要保证汉字不被截半个,如"我ABC"4, 应该截为"我AB",输入 ...

  3. 给定一个字符串,判断该字符串中是否包含某个子串.如果包含,求出子串的所有出现位置. 要求:从键盘输入两个字符串,第一个是给定的字符串,第二个是子串。

    给定一个字符串,判断该字符串中是否包含某个子串.如果包含,求出子串的所有出现位置. 要求:从键盘输入两个字符串,第一个是给定的字符串,第二个是子串. 输入 abcd23abc34bcd bc 输出 1 ...

  4. python字符串find函数-Python内置的字符串处理函数整理

    str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str) 例:print '%s length ...

  5. python内置字符串处理函数_Python内置的字符串处理函数

    生成字符串变量 str='python String function' 字符串长度获取:len(str) 例:print '%s length=%d' % (str,len(str)) 连接字符串 ...

  6. Java中找出s字符串的回文_给定一个字符串 s,找到 s 中最长的回文子串。

    题目描述:给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 思路: 判断s[i..j]是否是回文字符串,依赖于s[i+1...j-1],这种一个问题的结果依赖于 ...

  7. c语言函数返回值可以是字符串吗,函数返回值可以是字符串吗

    c语言中函数返回值可以是数组.字符串和结构体吗? 因为在C语言中函数不能返回数组,但字符串是存储在字符数组中的,所以能C语言中实现函数返回字符串,首先要确定函数返回的字符串地址的来源,一般分为四种方式 ...

  8. python 字符串函数split_字符串截取函数split(python截取字符串)

    问题说split 的用发明: 我知道c里有个分割字符串的函数,我现在要你们自己写一个给我,不要用 最佳中域教育李腾飞 巴菲特案因为字符串里的\\到了split里,需要分别转义,那么自然就需要用\\ \ ...

  9. php 字符串转时间戳_php如何将时间字符串转换为时间戳

    php将时间字符串转换为时间戳的方法:可以利用内置函数strtotime()来实现.strtotime()函数可以将任何英文文本的日期或时间描述解析为Unix时间戳,若成功则返回时间戳,否则返回fal ...

最新文章

  1. Linux网络服务-Web Service之【HTTP协议简介】(一)
  2. IT公司100题-10-翻转句子中单词的顺序
  3. 整型(int)转时间格式字符串及页面long型转时间格式字符串
  4. 从源码层面带你实现一个自动注入注解
  5. chrome只能装在c盘吗_电脑硬盘使用与文件整理入门——逃离C盘?
  6. linux centos7 r安装,如何在CentOS 7上安装R
  7. swing 显示文件下文件_Linux 文件权限详解
  8. python如何处理spark上的数据_Pyspark获取并处理RDD数据代码实例
  9. POJ 2263 Heavy Cargo 多种解法
  10. linux内核imx6,imx6ull之linux内核移植
  11. 前景菜谱制作的seo技巧
  12. 团队冲刺第一阶段第九天
  13. 交叉熵反向求导计算过程
  14. UIDynamic(物理仿真)
  15. 获取Map集合中数据的方法
  16. 金盾播放器android安卓,金盾跨平台视频加密系统
  17. Latex表格/公式在线编译转换器
  18. cadence 通孔焊盘_Allegro PCB -通孔焊盘制作 及Flash制作
  19. 初中英语语法(017)-关系代词
  20. 2020年计算机二级报名时间上半年,2020年上半年计算机二级报名时间是什么时候...

热门文章

  1. mysql二进制大文件_Mysql实例Mysql LONGTEXT 类型存储大文件(二进制也可以) (修改+调试+整理)...
  2. 杭电考研计算机专业课_杭州电子科技大学计算机考研专业课平均分76.03,国家线复试是这个原因!...
  3. antd form 初始化时间
  4. spring data jpa 分页查询
  5. 单独使用 laydate 日期时间组件
  6. elementUI清空弹框中的表单数据
  7. POI获取WORD信息
  8. CentOS6.5安装MySQL5.7详细教程
  9. AIX下RAC搭建 Oracle10G(六)dbca建库
  10. Design a high performance cache for multi-threaded environment