日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

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

發布時間:2023/12/1 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php字符串反转函数_PHP | 反转给定的字符串而不使用库函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 stringreturn $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 | 反转给定的字符串而不使用库函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。