rjangelov

StringsInPHP - //Problem 6. Combine List of Letters

Jul 1st, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. //Problem 6.    Combine List of Letters
  2. //Write a script  that reads two array of letters l1 and l2 and combines them: appends all letters c from l2 to the end of l1, but only //when c does not appear in l1. Print the obtained combined list. All lists are given as sequence of letters separated by a single space, //each at a separate line. Use ArrayList<Character> of chars to keep the input and output lists
  3.  
  4. <?php
  5. //$str1=array('h', 'e', 'l', 'l', 'o');
  6. //$str2=array('l','o','w');
  7. $str1=array('a', 'b', 'c', 'd');
  8. $str2=array('x','y','z');
  9. for($i=0;$i<=count($str2)+1;$i++){
  10.     for($j=0;$j<=count($str1)+1;$j++){
  11.         if ($str1[$j]==$str2[$i]) {  
  12.             $flag=0;
  13.             break;            
  14.         }
  15.         elseif($str1[$j]!=$str2[$i]){
  16.             $flag++;
  17.         }
  18.     }
  19.     if($flag!=0){
  20.         $str1[]=$str2[$i];
  21.     }
  22.     $flag=0;
  23. }
  24. echo '<pre>' . print_r($str1, true) . '</pre>';
  25. ?>
Advertisement
Add Comment
Please, Sign In to add comment