views:

330

answers:

2

Hi,

(my first post was not clear and confusing so I've edited the question)

I was studying string manipulation. You can use strlen() or substr() but cannot rely on other functions that are predefined in libraries.

Given string $string = "This is a pen", remove "is" so that return value is "Th a pen" (including 3 whitespaces).

Remove 'is' means if a string is "Tsih", we don't remove it. Only "is" is removed.

I've tried (shown below) but returned value is not correct. I've run test test and I'm still capturing the delimiter.

Thanks in advance!

function remove_delimiter_from_string(&$string, $del) {
    for($i=0; $i<strlen($string); $i++) {
        for($j=0; $j<strlen($del); $j++) {
            if($string[$i] == $del[$j]) {
                $string[$i] = $string[$i+$j]; //this grabs delimiter :(
            }
        }
    }
    echo $string . "\n";
}
+1  A: 

If you were allowed to use substr() it'd be so much easier. Then you could just loop it and check for the matched value, why can't you use substr() but you can strlen() ?

But without, this works at least:

echo remove_delimiter_from_string("This is a pen","is");

function remove_delimiter_from_string($input, $del) {
    $result = "";
    for($i=0; $i<strlen($input); $i++) {
        $temp = "";
        if($i < (strlen($input)-strlen($del))) {
            for($j=0; $j<strlen($del); $j++) {
                $temp .= $input[$i+$j];
            }
        }
        if($temp == $del) {
            $i += strlen($del) - 1;
        } else {
            $result .= $input[$i];
        }
    }
    return $result;
}
FrieK
If you can use substr() then you can simplify the code by a lot.Then you can basically replace the whole $del loop by a simple substr()
FrieK
+2  A: 

Clarifying, the original quiestion is not Implement a str_replace, It's remove 'is' from 'this is a pen' without any functions and no extra white spaces between words. The easiest way would be $string[2] = $string[3] = $string[5] = $string[6] = '' but that would leave an extra white space between Th and a (Th[ ][ ]a).

There you go, no functions at all

$string = 'This is a pen';
$word = 'is';
$i = $z = 0;

while($string[$i] != null) $i++;
while($word[$z] != null) $z++;

for($x = 0; $x < $i; $x++) 
 for($y = 0; $y < $z; $y++)
  if($string[$x] === $word[$y])
   $string[$x] = '';
Ben
If I have the string as "That's not a pen", this function returns "That' not a pen"? It's not quite that simple... Also, setting the character to "" like that sets the ASCII character to an unknown UTF-8 character, and when outputted will produce "Th�� �� a pen" and "That'� not a pen" respectively.
animuson
This doesn't work. `Thias` and `is` gives `Tha` but it shouldn't replace anything.
Anurag
@animuson, @Anurag - It replaces the characters in the replace word,e.g: `"is"` will remove all `s's` and `i's`. The question was `"Given string $string = 'This is a pen', remove 'is' so that return value is 'Th a pen'"` not `"Implement a str_replace function"`.
Ben
It sounded to me like it was supposed to remove the entire word, not the letters in the word. It doesn't specify either way. Your comment doesn't address the invalid characters, however.
animuson
@animuson - C'mon, it's an interview question. When I used to solve algorithms in college tests, with pencil and paper, I hardly thought about character encodings.
Ben