0

I am looking for an algorithm in PHP to make output of all possibilities with dot. produces we can put do in any place of word but repeated two dots after each other is now allowed. for example Shaghayegh output like below:

S.haghayegh
Sh.aghayegh
Shag.hayegh
Sh.agha.ye.gh

output that is not allowed:

s..haghayegh (repeat dots right after each other)
.shaghayegh (put dots at first of word)
shaghayegh. (put dots at end of word)
  • 1
    You will probably have to code it yourself. Welcome to Stack Overflow! Please see [How to Ask](http://stackoverflow.com/questions/how-to-ask) and [The perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Take a moment to read through the [editing help](http://stackoverflow.com/editing-help) in the help center. – JimL Apr 24 '16 at 19:56
  • This would be very easy! – Praveen Kumar Purushothaman Apr 24 '16 at 19:57
  • Check this out: http://stackoverflow.com/questions/36791679/php-array-push-element-at-an-index-and-move-the-previous-element/36792040#36792040 – Praveen Kumar Purushothaman Apr 24 '16 at 20:07

1 Answers1

0

Refer this answer of mine, and I have solved:

<?php
    header("Content-type: text/plain");
    $var = "Shaghayegh";
    for ($i = 1; $i < strlen($var); $i++) {
        $avar = str_split($var);
        array_splice($avar, $i, 0, array("."));
        echo "$i: " . implode("", $avar) . "\n";
    }
?>

The output will be:

1: S.haghayegh
2: Sh.aghayegh
3: Sha.ghayegh
4: Shag.hayegh
5: Shagh.ayegh
6: Shagha.yegh
7: Shaghay.egh
8: Shaghaye.gh
9: Shaghayeg.h

If I change it to Praveen, it gives:

1: P.raveen
2: Pr.aveen
3: Pra.veen
4: Prav.een
5: Prave.en
6: Pravee.n
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226