w3resource

PHP Exercises: Remove the character in a given position of a given string

PHP Basic Algorithm: Exercise-6 with Solution

Write a PHP program to remove the character in a given position of a given string. The given position will be in the range 0..string length -1 inclusive.

Sample Solution:

PHP Code :

<?php
// Define a function named "test" that takes two parameters, $s and $n
function test($s, $n) 
{
    // Use the substr function to concatenate the substring of $s from index 0 to $n-1
    // with the substring of $s from index $n+1 to the end of the string
    return substr($s, 0, $n) . substr($s, $n + 1, strlen($s) - $n);
}

// Call the test function with arguments "Python" and 1, then echo the result
echo test("Python", 1) . "\n";

// Call the test function with arguments "Python" and 0, then echo the result
echo test("Python", 0) . "\n";

// Call the test function with arguments "Python" and 4, then echo the result
echo test("Python", 4) . "\n";
?>

Explanation:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/basic-algorithm/php-basic-algorithm-exercise-6.php