0
<?php

   $first="rahul";
  
   $second="shiva";

   for($i=0;$i<strlen($first);$i++)
       {
          for($j=0;$j<strlen($second);$j++)
             {
                 if($first[$i]==$second[$j])
                  {
                        
                    $first= "".substr($first,0,$i).substr($first,$i+1,strlen($first));

                        $second= "".substr($second,0,$j).substr($second,$j+1,strlen($second));
                      
                        $i--;
                                           
                        $j--; 
                     
                   }


              }
              

        }  
$len= Strlen($first) + strlen($second);
        $f="friends";
         $duplen=1;
  for($i=0;(strlen($f)!=1);)
  {


                if($len!=$duplen)
            {

                      if($i==(strlen($f)))
                    {


                         $i=0;



                     }

                     else
                     {
                         $i++;
                    }
                     $duplen++;           
}

else
      {

           $f=substr($f,0,$i).substr($f,$i+1,strlen($f)-1);

     }



   }


   
?>

i am not finding any mistake in the above code but its not getting executed.
its giving an error
"Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\check.php on line 59 "
i hope i will be helped pls

Edited by sharathg.satya: n/a

4
Contributors
27
Replies
28
Views
6 Years
Discussion Span
Last Post by diafol
0

I'm not really sure what you're trying to do, but perhaps you want to add a break after that line (now line 60, I guess?).

0

yes i got struck at that 60 th line
can i know why that's happening....
please let me know

Edited by sharathg.satya: n/a

0

It's just a quick guess, that php's iterating over that line over and over again, try to add break; after that line. If this doesn't help, what is it that you're trying to do?

0

Readable:

$first="rahul";
$second="shiva";
 
for($i=0;$i<strlen($first);$i++){
	for($j=0;$j<strlen($second);$j++){
		if($first[$i]==$second[$j]){
			$first= "".substr($first,0,$i).substr($first,$i+1,strlen($first));
			$second= "".substr($second,0,$j).substr($second,$j+1,strlen($second));
			$i--;
			$j--; 
		}
	}
}  
$len= Strlen($first) + strlen($second);
$f="friends";
$duplen=1;
for($i=0;(strlen($f)!=1);){
	if($len!=$duplen){
		if($i==(strlen($f))){
			$i=0;
		}else{
			$i++;
		}
		$duplen++;           
	}else{
		$f=substr($f,0,$i).substr($f,$i+1,strlen($f)-1);
	}
}

Looking at that, your loops look really suspect.

Edited by diafol: n/a

0

usage of break didnt help me sir.
the concept is
i am havng two names out of them i have to remove the common lettters and calculate the length of the remaining words.
this length is stored in the variable len
and using this len variable i am counting the word 'friends'
for example
if length is 4
letter e of the word friends is removed and the new word obtained wil be jst 'frinds'
in the same way my code has to work till only single letter of the word friends wil remain..
this is the concept i wish to implement.
please help me in this

Edited by sharathg.satya: n/a

0

So you have 2 strings and want to calculate their length if you remove the letters that are in both?

0

Here i have made a script.
I think this is exactly what you want.
Try and give me feedback.

<?
	$first="rahul";
	$second="shiva";
	echo "<h2>$first & $second</h2>";
   	$len= strlen($first) + strlen($second);
 
	for($i=0;$i<strlen($first);$i++){
		for($j=0;$j<strlen($second);$j++){
			if($first[$i]==$second[$j])
			{
				echo '<br />Letter common : '.$first[$i];
				$len=$len-2;
			}
		}
	}	
	$frd = explode(" ","F R I E N D S");
	echo '<br />Remaining Length : '.$len;
	echo '<br /><br />';
	$cnt = 0;
	while(count($frd)!=1)
	{
		if($len>count($frd))
		{
			$de = ($len % (count($frd)));
			if($de == 0)
				$de = count($frd);
			$l = $de -1;
		}
		else
			$l = $len-1;
		
		echo '<br />'.implode('',$frd).' Cross on '.$frd[$l];
		unset($frd[$l]);
		
		$frd = explode('#',implode('#',$frd));
		echo ', Remaining '.implode('',$frd);	
		$cnt++;		
	}
	echo '<h2>Answer is : '.implode('',$frd).'</h2>';
	
?>
0

If you just want to get the common characters:

$ans = array_intersect(array_unique(str_split($string1,1)),array_unique(str_split($string2,1)));

Or for multibyte chars:

$ans = array_intersect(array_unique(preg_split('/(?<!^)(?!$)/u', $string1 )),array_unique(preg_split('/(?<!^)(?!$)/u', $string2 )));

But I suspect you need something a little more complicated?

If you need no. characters not used:

$strlen1 = mb_strlen($string1,'UTF-8') - count($ans);

I'm not sure about the output you need:

echo "Common chars = " . implode("",$ans);

Edited by diafol: n/a

0

Have you tried this ????

Here i have made a script.
I think this is exactly what you want.
Try and give me feedback.

<?
	$first="rahul";
	$second="shiva";
	echo "<h2>$first & $second</h2>";
   	$len= strlen($first) + strlen($second);
 
	for($i=0;$i<strlen($first);$i++){
		for($j=0;$j<strlen($second);$j++){
			if($first[$i]==$second[$j])
			{
				echo '<br />Letter common : '.$first[$i];
				$len=$len-2;
			}
		}
	}	
	$frd = explode(" ","F R I E N D S");
	echo '<br />Remaining Length : '.$len;
	echo '<br /><br />';
	$cnt = 0;
	while(count($frd)!=1)
	{
		if($len>count($frd))
		{
			$de = ($len % (count($frd)));
			if($de == 0)
				$de = count($frd);
			$l = $de -1;
		}
		else
			$l = $len-1;
		
		echo '<br />'.implode('',$frd).' Cross on '.$frd[$l];
		unset($frd[$l]);
		
		$frd = explode('#',implode('#',$frd));
		echo ', Remaining '.implode('',$frd);	
		$cnt++;		
	}
	echo '<h2>Answer is : '.implode('',$frd).'</h2>';
	
?>
0

i have less knowledge in php
i am not understanding the functions u have used in the program
please can i know them

0

Can you create test.php and copy all my above code and run it in your browser.
And post whether your output is correct or not??

0

yes sir its right
can i know the site where i can find out those functions please
i have no knowledge about those functions
and
what's the wrong with my code?

0

Find my inline comments.
Your logic and loop was messy and i didn't get it.
So i have recreated code.
For any php function help visit http://php.net/

<?
	$first="rahul";
	$second="shiva";
	echo "<h2>$first & $second</h2>";
   	$len= strlen($first) + strlen($second); // total no of letter in both words
 	
	// in this for loop we will check for matching  letters
	for($i=0;$i<strlen($first);$i++){
		for($j=0;$j<strlen($second);$j++){
			if($first[$i]==$second[$j])
			{
				echo '<br />Letter common : '.$first[$i];
				$len=$len-2; // two letters found common so 2 letters removed from total count
			}
		}
	}	
	$frd = explode(" ","F R I E N D S"); // created array for FRIENDS iteration
	echo '<br />Remaining Length : '.$len;
	echo '<br /><br />';
	$cnt = 0;
	while(count($frd)!=1) // untill we get only one element from FRIENDS do the loop
	{
		// this if-else loop will check the location which will be cut from FRIENDS
		if($len>count($frd))
		{
			$de = ($len % (count($frd)));
			if($de == 0)
				$de = count($frd);
			$l = $de -1;
		}
		else
			$l = $len-1;
		
		echo '<br />'.implode('',$frd).' Cross on '.$frd[$l];
		unset($frd[$l]); // removing crossed/cut element from FRIENDS array
		
		$frd = explode('#',implode('#',$frd));
		echo ', Remaining '.implode('',$frd);	
		$cnt++;		
	}
	echo '<h2>Answer is : '.implode('',$frd).'</h2>';
	
?>
0

Erm, it's the string you want to delimit the substrings with (kinda) so

explode(' ', 'hello world here I come');

becomes

array('hello','world','here','I','come');

Anyway, I don't understand that line either. vibhadevit, what's the logic in line 37?

0

I had a few minutes on my hands:

//====================================================================//
//======================== commonChars Function ======================//
//====================================================================//

//  last optional parameter ($i) can take two values:
//  1 = compare lowercase versions of strings
//  2 = compare uppercase versions of strings
//  otherwise comparison is case-sensitive

function commonChars($string1,$string2,$i=0){
	if($i == 1){ 		//change to lowercase
		$string1 = mb_strtolower($string1,'UTF-8');
		$string2 = mb_strtolower($string2,'UTF-8');
	}elseif($i == 2){	//change to uppercase
		$string1 = mb_strtoupper($string1,'UTF-8');
		$string2 = mb_strtoupper($string2,'UTF-8');
	}
	$r_string1 = array_unique(preg_split('/(?<!^)(?!$)/u', $string1 ));
	$r_string2 = array_unique(preg_split('/(?<!^)(?!$)/u', $string2 ));
	$i_string1 = count($r_string1);
	$i_string2 = count($r_string2);
	
	$output = 	"<h3>Comparing $string1 and $string2 &hellip;</h3>";
	
	$common = array_intersect(array_unique($r_string1),array_unique($r_string2));
	$i_common = count($common);
	if($i_common == 0){
		$output .= "<p>No common characters found for $string1 and $string2.</p>";
	}else{
		$out_string1 = $i_string1 - $i_common;
		$out_string2 = $i_string2 - $i_common;
		$r_common = implode(",",$common);
		$pluralc = ($i_common > 1) ? 's' : '';
		$plural1 = ($i_string1 > 1) ? 's' : '';
		$plural2 = ($i_string2 > 1) ? 's' : '';
		
		$output .= 	"<p>For $string1: $i_common character$pluralc from $i_string1 unique character$plural1.</p>" .
				"<p>For $string2: $i_common character$pluralc from $i_string2 unique character$plural2.</p>" .
				"<p>The common characters were $r_common</p>";
	}
	return $output;
}



$str1 = "Österreich";
$str2 = "öocyte";
//Note the use of multibyte characters

//====================================================================//
//====================== sample usage of function ====================//
//====================================================================//

echo commonChars($str1,$str2);		//case-sensitive

echo commonChars($str1,$str2,1);	//compare using lowercase conversion

echo commonChars($str1,$str2,2);	//compare using uppercase conversion

Edited by diafol: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.