0

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 the returned value is not correct. I've run test test and I'm still capturing the delimiter.

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";
}
6
  • 3
    Are you the interviewer or interviewee? If you are the interviewee, I think they are looking for your ability to answer this question, not the community's. Commented Jun 1, 2010 at 4:06
  • With these types of problems it helps to walk through each step of your solution with an example string such as the one you provided. Commented Jun 1, 2010 at 4:16
  • 1
    Isn't strlen() a predefined function? If you can use strlen() then you ought to be able to use substr() too... Commented Jun 1, 2010 at 4:16
  • 1
    animuson is right, you should iterate trough the string to count the characters instead of using strlen. Commented Jun 1, 2010 at 4:24
  • This just makes no sense to me. Surely str_replace() is just as much a part of PHP's standard library as is strlen(). You just use $string = str_replace('is', '', $string);. The hardest part is remembering which order the arguments go in and whether there's an underscore in the function name (PHP!!!) Also, in the expected output, is there supposed to be only one space in between "Th" and "a"? Or are there supposed to be two? If there's supposed to be only 1 then the problem is slightly more complicated, but only slightly. Commented Jun 1, 2010 at 4:32

3 Answers 3

2

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] = '';
Sign up to request clarification or add additional context in comments.

5 Comments

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.
This doesn't work. Thias and is gives Tha but it shouldn't replace anything.
@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".
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 - 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.
1

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;
}

1 Comment

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()
0

The following code can also used to replace the sub string:

$restring = replace_delimiter_from_string("This is a pen","is", "");
var_dump($restring);
$restring = replace_delimiter_from_string($restring,"  ", " ");
var_dump($restring);

function replace_delimiter_from_string($input, $old, $new) {
    $input_len = strlen($input);
    $old_len = strlen($old);
    $check_len = $input_len-$old_len;

    $result = "";
    for($i=0; $i<=$check_len;) {
        $sub_str = substr($input, $i, $old_len);
        if($sub_str === $old) {
            $i += $old_len;
            $result .= $new;
        }
        else {
            $result .= $input[$i];
            if($i==$check_len) {
                $result = $result . substr($input, $i+1);
            }
            $i++;
        }
    }
    return $result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.