1. Verwenden Sie die integrierten Funktionen von PHP, um
zu implementieren
Testcode
<code><span><span><?php</span><span>$str</span>=<span>'Hello World!'</span>; <span>$res</span>=strrev(<span>$str</span>); <span>echo</span><span>$res</span>.<span>'</br>'</span>; <span>$str</span>=<span>'Hello World!你好 世界!'</span>; <span>$res</span>=reverse(<span>$str</span>); <span>echo</span><span>$res</span>; <span>//自定义函数</span><span><span>function</span><span>reverse</span><span>(<span>$str</span>)</span> {</span><span>$len</span> = mb_strlen(<span>$str</span>, <span>'utf-8'</span>); <span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> < <span>$len</span>; <span>$i</span>++) { <span>$arr</span>[] = mb_substr(<span>$str</span>, <span>$i</span>, <span>1</span>, <span>"utf-8"</span>); } <span>return</span> implode(<span>""</span>, array_reverse(<span>$arr</span>)); } <span>?></span></code>
Ergebnisse ausführen
!dlroW olleH!
Hallo Welt Welt!dlroW olleH
2.c Sprachimplementierung
<code><span>#<strong>include</strong> <stdio.h></span><span>#<strong>include</strong><string.h></span><span>char</span> *<span>str</span>=<span>"Hello World!你好 世界!"</span>; <span>int</span> main() { <span>int</span> i = strlen(<span>str</span>) - <span>1</span>; <span>while</span> (i>=<span>0</span>) { <span>if</span>(<span>str</span>[i] < <span>0</span>) { printf(<span>"%c%c%c\n"</span>, <span>str</span>[i-<span>2</span>], <span>str</span>[i-<span>1</span>],<span>str</span>[i]); i = i-<span>2</span>; }<span>else</span>{ printf(<span>"%c\n"</span>, <span>str</span>[i]); } i--; } <span>return</span><span>0</span>; }</code>
Das Obige stellt die PHP-Zeichenumkehr vor, einschließlich des Include-Inhalts. Ich hoffe, es wird für Freunde hilfreich sein, die sich für PHP-Tutorials interessieren.