Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Wednesday, 26 August 2020

PHP Program Reverse the String without Function

PHP Program Reverse the String without Function


 Write a PHP  function FirstReverse(str) take the str parameter being passed and return the string in reversed order. 

    For Example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH.

Solution:


function FirstReverse($str) {
    $result='';
    for($i=strlen($str)-1; $i>=0; $i--){
      $result = $result.$str[$i];
    }  
      return $result;

}



PHP Program Find Intersection between Two array

PHP Program Find Intersection between Two array

Create PHP  function FindIntersection(strArr) read the array of strings stored in strArr which will contain 2 element

 the first element will represent a list of comma-separated numbers sorted in ascending order.
 the second element will represent a second list of comma-separated numbers. 

Your function should return a comma-separated string containing the numbers that occur in elements of strArr in sorted order.
  If there is no intersection, return the string false.


Solution:

  function FindIntersection($strArr) {
    $return ='false';  
    $array1 = explode(',',$strArr[0]);
    $array1= array_map('trim',$array1);

    $array2 = explode(',',$strArr[1]);
    $array2= array_map('trim',$array2);

    $result= array_intersect($array1,$array2);
    if(!empty($result)){
      $return=implode(',',$result);  
    }

  return $return;

}
  

PHP Program Longest Word from the string

PHP Program Longest Word from the string

 Create PHP function LongestWord(sen) take the sen parameter being passed and return the largest word in the string.

If there are two or more words that are the same length, return the first word from the string with that length. 

Ignore punctuation and assume sen will not be empty.




Solution: 


function LongestWord($sen) {
    $return = '';

    //Convert string into Array
    $strArrayList = explode(' ', $sen);
    $finalArray = array();

    ///////Filter the words///////////
    foreach($strArrayList as $str){
        $str = preg_replace("#[[:punct:]]#", "", $str);
        $finalArray[] = $str;
    }
    ///////Filter the words///////////
    
    /////////// Get the First word  from the string with that length////////
    foreach($finalArray as $word){
        if(strlen($word)> strlen($return)){
        $return = $word;
        }
    }
    /////////// Get the First word  from the string with that length////////
    
    
    return $return;

}


Tuesday, 25 August 2020

PHP Username Validation program

 

PHP Username Validation program

Creat function usernameValidation(str) take the str parameter being passed and determine if the string is a valid username according to the following rules:
  1. The username is between 4 and 25 characters.
  2. It must start with a letter.
  3. It can only contain letters, numbers, and the underscore character.
  4. It cannot end with an underscore character.
If the username is valid then your program should return the string true, otherwise return the string false

Solution:
function usernameValidation($str) {
    $return = true;
    
    ////////////////// 4 to 25 character////////////
    if ($return) {
        if (!(strlen($str) >= 4 && strlen($str) <= 25)) {
            $return = false;
        }
    }
    ////////////////// 4 to 25 character////////////
    
    ////////////////// Must start with Number ////////////
    if ($return) {
        $asciiCode = ord($str[0]);
        if (!(($asciiCode >= 97 && $asciiCode <= 122) || ($asciiCode >= 65 && $asciiCode <= 90))) {
            $return = false;
        }
    }
    ////////////////// Must start with Number ////////////
    
    ///////////// It can only contain letters, numbers, and the underscore character.///////////
    if ($return) {
        for ($i = 0; $i < strlen($str); $i++) {
            $asciiCode = ord($str[$i]);
            if (!( ($asciiCode >= 97 && $asciiCode <= 122) || ($asciiCode >= 65 && $asciiCode <= 90) || ($asciiCode >= 48 && $asciiCode <= 57) || ($asciiCode == 95))) {
                $return = false;
                break;
            }
        }
    }
    ///////////// It can only contain letters, numbers, and the underscore character.///////////
    
    ///////////It cannot end with an underscore character.///////////
    if ($return) {
        $asciiCode = ord($str[$i - 1]);
        if ($asciiCode == 95) {
            $return = false;
        }
    }
    ///////////It cannot end with an underscore character.///////////
    
    return $return ? 'true' : 'false';
}