Delete an Element from Array Using unset Function in PHP

AmitDiwan
Updated on 17-Aug-2020 12:01:57

390 Views

To delete an element from the array using the unset function, the PHP code is as follows −Example Live DemoOutputAfter deleting the element, the array isArray (    [0] => Joe [1] => Ben [2] => Mary [3] => Barun [5] =>    Mona )Above, an array is defined, with multiple strings −$my_array = array("Joe", "Ben", "Mary", "Barun", "Sona", "Mona");The unset function is used, by specifying the index of the element in the array that needs to be deleted. After the element is deleted, the output/array is printed on the screen −unset($my_array[4]); print_r("After deleting the element, the array is"); print_r ($my_array);

Find Length of the Last Word in a String using PHP

AmitDiwan
Updated on 17-Aug-2020 12:00:20

989 Views

To find the length of the last word in the string, the PHP code is as follows −Example Live DemoOutputThe length of the last word is 3 The length of the last word is 6A PHP function named ‘last_word_len’ is defined, that takes a string as the parameter −function last_word_len($my_string) { // }The first occurrence of the space inside another string is found by using the ‘strrpos’ function. If that position is present, it is assigned to 0. Otherwise, it is incremented by 1 −$position = strrpos($my_string, ' '); if(!$position){    $position = 0; } else{    $position ... Read More

Find the First Word of a Sentence in PHP

AmitDiwan
Updated on 17-Aug-2020 11:57:19

827 Views

To find the first word of a sentence, the PHP code is as follows −Example Live DemoOutputThe first word of the string is HiA string is defined at first −$my_string = 'Hi there, this is a sample statement';The ‘strtok’ function is an in-built function that is used to split a string into specified number of parts. Before the first space occurs, the string needs to be split. This split string’s first part is displayed as the output on the screen −echo "The first word of the string is ". strtok($my_string, " ");

Find Number of Characters in a String in PHP

AmitDiwan
Updated on 17-Aug-2020 11:55:55

996 Views

To find the number of characters in the string, the PHP code is as follows −Example Live DemoOutputThe number of characters in the string is 27Above, a string is defined that has more than the required spaces in between −$my_string = "Hi there, this is a sample ";Next, the length of the string is computed using the ‘strlen’ function. This length is assigned to a variable. This will give the characters present in the string including the spaces as well −$len = mb_strlen($my_string);The length computed is then printed on the screen −print_r("The number of characters in the string is "); echo ... Read More

PHP Program to Sum the Digits in a Number

AmitDiwan
Updated on 17-Aug-2020 11:54:11

4K+ Views

To sum the digits in a number, the PHP code is as follows −Example Live DemoOutputThe sum of digits is 11Above, a function named ‘sum_of_digits’ is defined, that takes an integer as a parameter.$my_num = "65";It first declares the variable sum as 0 and then iterates through the length of the declared integer, and adds the element at the ‘i’th place to the sum. Once the iteration is complete, this function returns the sum as output. Outside the function, the value is declared and the function is called by passing this element as the parameter −function sum_of_digits($my_num){    $sum = 0; ... Read More

Check if a String Has a Special Character in PHP

AmitDiwan
Updated on 17-Aug-2020 11:31:47

8K+ Views

To check if a string has a special character, the PHP code is as follows;Example Live DemoOutputString has not been acceptedAbove, a function named ‘check_string’ is defined, that takes a string as its parameter −$my_string = 'This_is_$_sample!';Use regular expression to check if a string has a special character. If there is a special character, a specific message is printed. Outside the function, the string is defined, and the function is called by passing the string as parameter −function check_string($my_string){    $regex = preg_match('[@_!#$%^&*()?/|}{~:]', $my_string);    if($regex)       print("String has been accepted");    else       print("String has not ... Read More

Replace a Word with a Different Symbol in a Sentence Using PHP

AmitDiwan
Updated on 17-Aug-2020 11:25:33

227 Views

To replace a word with a different symbol in a sentence, the PHP code is as follows −Example Live DemoOutputThe final replaced string is : is a simple onlyHere, a string is defined, and the string that needs to be replaced with a different string is placed inside an array and arranged to a variable −$my_str = "This is a sample only"; $search_str = array("sample", "This"); $replace_str = array("simple");The ‘str_replace’ function is used to replace the value with a different specified value. The result is printed on the screen −$result = str_replace($search_str, $replace_str, $my_str); print_r("The final replaced string is :"); print_r($result);Read More

Sum of Two Numbers with One Number as Array of Digits in C++

sudhir sharma
Updated on 17-Aug-2020 10:43:45

170 Views

In this problem, we are given two numbers, from which one is represented using array of digits. Our task is to create a program that will find the sum of two numbers where one number is represented as array of digits.Let’s take an example to understand the problem, Input: n = 213, m[] = {1, 5, 8, } Output: 371 Explanation: 213 + 158 = 371To solve this problem, we will simply digit by digit from the number which element of the array. It lsb of the number is added to the (n-1)th element of the array. The carry will ... Read More

Sum of Two Large Numbers in C++

sudhir sharma
Updated on 17-Aug-2020 10:40:28

5K+ Views

In this problem, we are given two string that defines two large numbers. Our task is to create a program to find the sum of two large numbers.Let’s take an example to understand the problem, Input: number1 = “341299123919” number2 = “52413424” Output: 341351537343To solve this problem, we will traverse both the string. And add digit by digit and propagate the carry. And store the result digit by digit to sum string.AlgorithmInitialize sum = 0, carry = 0. Step 1: loop from n to 0. Step 1.1: intSum = number1[i] + number2[i] Step 1.2: carry = intSum/10. Sum += intSum ... Read More

Sum of the Series k^n + k^(n-1) + k^(1) + 1 + k^(n-2) + k^(1) + 2 + k^(1) + n in C++

sudhir sharma
Updated on 17-Aug-2020 10:22:15

331 Views

In the problem, we are ginen two number k and n of the series K^n + ( K^(n-1) * (K-1)^1 ) + ( K^(n-2) * (K-1)^2 ) + ... (K-1)^n. Our task is to create a program to find the sum of the series.Let’s take an example to understand the problem, Input: n = 3, k = 4 Output: 175 Explanation: Sum of the series is = 4^3 + ( (4^2)*(3^1) ) + ( (4^1)*(3^2) ) + ( (4^0)*(3^3) ) = 64 + 48 + 36 + 27 = 175A simple way to solve the problem, is using a for ... Read More

Advertisements