显示字符串的可能组合

编程入门 行业动态 更新时间:2024-10-08 06:28:59
本文介绍了显示字符串的可能组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图获取一个字符串并显示它的可能组合(在PHP中),但同时按每个单词的顺序说.例如:你好吗"将返回(一个数组)

I am trying to take a string and display the possible combinations of it (in PHP), but while saying in order of each word. For example: "how are you" would return (an array)

How are you How are are you how you are

我现在拥有的代码可以显示所有组合,但是我希望它能使它们保持顺序,而不是乱七八糟.任何人都有他们想分享的想法或摘要吗?谢谢

The code I have now displays all combinations but I am wanting it to keep them in order and not flip words. Any one have any ideas or snippets they care to share? Thanks

推荐答案

设置两个迭代器,并在它们之间打印所有内容.像这样:

Set two iterators and print everything between them. So something like this:

<? $str = "How are you"; $words = explode(" ",$str); $num_words = count($words); for ($i = 0; $i < $num_words; $i++) { for ($j = $i; $j < $num_words; $j++) { for ($k = $i; $k <= $j; $k++) { print $words[$k] . " "; } print "\n"; } } ?>

输出

How How are How are you are are you you

更多推荐

显示字符串的可能组合

本文发布于:2023-11-30 07:31:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!