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
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
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
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
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
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
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
In this problem, we are given an array arr[] of n numbers. Our task is to create a program to find the sum of XOR of all subarrays of the array.Here, we need to find all sub-arrays of the given array, and then for each subarray, we will find the xor of element and add the XOR value to the sum variable.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 4} Output: Explanation: XOR of all subarrays for the array : XOR {5} = 5 XOR {1} = 1 XOR {4} = 4 XOR {5, 1} ... Read More
In this problem, we are given an array aar[] of n numbers. Our task is to create a program to find the Sum of XOR of all possible subsets.Here, we will find all subsets of the array. Then for each subset, we will find the XOR of elements of the subset and add them to the sum variable.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 4} Output: 20 Explanation: XOR of all subsets: {5} = 5 {1} = 1 {4} = 4 {5, 1} = 4 {5, 4} = 1 {1, 4} = 5 {5, ... Read More
In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the sum of XOR of all pairs in an array.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 4} Output: 10 Explanation: the sum of all pairs: 5 ^ 1 = 4 1 ^ 4 = 5 5 ^ 4 = 1 sum = 4 + 5 + 1 = 10One simple approach to solve this problem is to run nested loops and find all pairs of numbers. Find XOR of each pair and add ... Read More