user3221719 user3221719 - 7 months ago 8
PHP Question

Generate all possibilities with dot

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)

Answer

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
Comments