Jump to content

Comparing two strings - Same letters, but different letter positions


gretorx

Recommended Posts

I've been working on a piece of code, but can't seem to wrap my head around it. It is supposed to perform the following steps:

 

1) Convert each character of user input to ascii value (eg. a = 97)

2) Adds the user input characters together  (eg. aaa = 97 + 97 + 97, which equals 291)

3) Sets that value as a variable

 

4) Opens a text file containing a list of words (eg. list.txt in this case)

5) Parses the contents of the text file into an array (eg. using the explode function, separating the words in list.txt by commas). This should make each word a separate item in the array.

6) Converts each character of each parsed word into ascii value (eg. just like step one)

7) Adds the values of the characters together (eg. just like step two)

 

8) And finally, returns the result of which word in the text file has the same value of the user input.

 

This serves as basically a dictionary unscrambler.

 

The code I have is as follows

 

<?php
$input=$_GET['string'];
$len=strlen($input);

$file="list.txt";

$contents=fopen($file, "r");

$read=fread($contents, filesize($file)); 
$parse = explode(', ', $read);


for ($j=0;$j<$len;$j++){
$char[$j]=ord($input[$j]);
$compare=array_sum($char);
}

for ($i=0;$i<filesize($file);$i++){
         
         for ($k=0;$k>len($parse2);$k++){
         $parse2=$parse[$i];
         $char[$k]=ord($parse2[$k]);
         $compare2=array_sum($char);
         }
}

if ($compare2=$compare1){
echo $input . "Contains the same ascii value as" . $parse2;
]
?>

<html>
<body>
<form method="get" action="SOP3.php">
<input type="text" name="string" value="">
<input type="submit" value="Submit">
</form>
</body>
</html>

 

I'm new to php if you can't tell, so I'm sure there are some simple mistakes in the code. Any help you could offer would be greatly appreciated.

Link to comment
Share on other sites

First off isn't there a logic problem to this? You could end up with different words using the same ascii sum value... for example 9+2+3  sum is the same as the sum of 2+12....

 

Neither here nor there though....

 

I'd use a str_split() function on the word after you've got the word.  Thus do a foreach to go through your arrays instead

 

Once you have your word I'd then split the word into separate strings with str_split()

<?php
$input=$_GET['string'];
$len=strlen($input);

$file="list.txt";

$contents=fopen($file, "r");

$read=fread($contents, filesize($file)); 
$parse = explode(', ', $read);

foreach ($parse as $value)
    {
        $String_split = str_split($value, 1);
        $Temp = 0; //resets itself before each word here
         foreach($String_split as $value_2)
         {
          $Temp = $Temp + ord($value_2); // adds each ascii value
         
         }

         if($Temp == $input)
          {
             echo $input . "Contains the same ascii value as" . $value_2;
             break;
           }
         else{$Temp = "NONE";}
    }

if($Temp == "NONE"){ echo "No Match Found.";}

Link to comment
Share on other sites

Thank you very much for your help... by checking out the foreach php function and using your code as a reference, I was able to do exactly what i wanted to do. What a learning experience. I really appreciate your help.

Link to comment
Share on other sites

Nope, didn't take long at all. New question though. I've been trying to load the user input as an array, but only the first word gets unscrambled. As you can see from the code, I've attempted to parse the user input the same way that the text file input is being parsed; however, after the code finds matches for the first word, it just stops.

 

Here is what I have:

 

<?php

//Reads the contents of the user input
$input = $_GET['string'];
$len = strlen($input);


//Reads the contents of the dictionary file and writes it to a variable
$file = "list.txt";
$contents = fopen($file, "r");
$read = fread($contents, filesize($file)); 


//Separates each word after a ',' and puts it into the $parse[] array
$parse = explode(', ', $read);
$parse2 = explode(', ', $input);




//Converts each character in the user input into numerical value, then multiplies the numbers 

together. 
foreach ($parse2 as $val)
   {
   $split=str_split($val, 1);
   $compare = 0;
         foreach ($split as $val2)
         {
         $compare=$compare+ord($val2);
         }
   }

foreach ($parse as $value)
   {
   $split2=str_split($value, 1);
   $compare2 = 0;
         foreach ($split2 as $value2)
         {
         $compare2=$compare2+ord($value2);
         if($compare2 == $compare)
           {
           echo $value . " Contains the same ascii value as " . $val . "<br><br>";
           break;
           }else{
           $compare2 = "none";
           }
         }
   }

if($compare == "none")
   { 
   echo "The value of " . $val . " is " . $compare . ", and does not have any 

matches<br><br>";
   }

?>

<html>
<body>
<form method="get" action="SOP3c.php">
<input type="text" name="string" value="">
<input type="submit" value="Submit">
</form>
</body>
</html>

 

Does anyone have any articles I can read on this? I'm all about learning, that's why I'm posting. Thank you again.

Link to comment
Share on other sites

Try posting the form and reading the $_POST variable.  Get doesn't work well with spaces.

 

<form method="post" action="script.php">

 

$input = $_POST['string'];

 

foreach ($parse2 as $val)
   {
   $split=str_split($val, 1);
   $compare = 0;
         foreach ($split as $val2)
         {
         $compare=$compare+ord($val2);
         }
   

foreach ($parse as $value)
   {
   $split2=str_split($value, 1);
   $compare2 = 0;
         foreach ($split2 as $value2)
         {
         $compare2=$compare2+ord($value2);
         if($compare2 == $compare)
           {
           echo $value . " Contains the same ascii value as " . $val . "<br><br>";
           break;
           }else{
           $compare2 = "none";
           }
         }
   }

  
if($compare == "none")
   { 
   echo "The value of " . $val . " is " . $compare . ", and does not have any 

matches<br><br>";
   }
}

 

I moved a semi-colon so that your 2 major foreach(s) would be nested.  *untested*

Link to comment
Share on other sites

$w1 = 'hello';
$w2 = 'oelhl';

$p1 = array_sum(array_map('ord', str_split($w1)));
$p2 = array_sum(array_map('ord', str_split($w2)));

if ($p1 === $p2) {
  echo 'equal';
} else {
  echo 'not equal';
}

//Output: equal

 

function str2int($word) {
  return array_sum(array_map('ord', str_split($word)));
}

function word_is_like($word, $like) {
  return str2int($word) === str2int($like);
}

$match = $_GET['match'];
$words = file('path/to/text/file.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
foreach ($words as $word) {
  if (word_is_like($word, $match)) {
    echo $word, ' equals ', $match, "<br>\n";
  }
}

 

[ot]Tell your teacher to come up with something more difficult[/ot]

Link to comment
Share on other sites

I usually have a difficult time understanding when to use 3 equal signs.  Why is it important here?

 

It isn't. I got in to an habit of using them to notify readers that I'm comparing two variables of the same type. I use == whenever I compare to an unknown source that may be or may not be a numeric string (in the latter it will return 0). Syntactical sugar :)

 

== equal

=== identical

Link to comment
Share on other sites

Yeah, I just figured that unless one was null they would be identical in this example.

 

Syntactical sugar

 

awesome.

 

So it would be good for

0 == null          <-true

0 === null        <-false

 

?

 

Yeah it all started because I didn't like 1 == true|1|'1'

Link to comment
Share on other sites

First off isn't there a logic problem to this? You could end up with different words using the same ascii sum value...

 

Hmm; iguanodon, mongolian, pakistani, wednesday, lightning, magnetize, nostalgic, pistachiomillimole, scrapbook and unsayable (to name a few examples) all have the same ASCII number total (964) as phpfreaks:-*

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.