Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAgon Karakashi
822 PointsHow do I write a script that finds the first character and its position that is different between 2 strings same length?
I need to write a script that finds the first character and its position that is different between two strings with the same length.
Can anyone help?
5 Answers
Eric Butler
33,512 PointsSorry, but I don't understand what your script needs to do. Could you say it again a different way?
Robert Walker
17,146 PointsI think what he means is two words the same length :
"longWordOne"
"longWordTwo"
Compare both of them and return the first characters that don't match, for the two words above it would be O and that would be 8 characters in starting from 0.
Not really sure how you would does this either or what the reason for needing to do it would be.
Would love to see if it can be done and what sort of use it has.
Ill give it a go and get back to you!
Robert Walker
17,146 Points$string1 = "longWordOne";
$string2 = "longWordTwo";
$string1array = str_split($string1);
$string2array = str_split($string2);
$stringD = array_diff($string1array, $string2array);
var_dump($stringD);
Result is:
array(2) { [8]=> string(1) "O" [10]=> string(1) "e" }
Just remembered array_diff that should now work and give you back all the differences in it.
EDIT:
Infact no this wouldn't work either! Erics works perfectly so would use that!
Eric Butler
33,512 PointsRobert beat me to it, but this should actually work fully:
<?php
// provide your strings.
$word1 = "Insert first string here";
$word2 = "Insert second string here";
// figure out how many characters your strings have.
$totalCharacters = strlen($word1);
// convert each string to an array of its characters
$array1 = str_split($word1);
$array2 = str_split($word2);
// loop through the array elements one at a time.
// each time, check to see if the first array element is the same as the second array element.
// if they're the same, it goes back around.
// if they're different, the character in both arrays and the character number ...
// are all set as variables, and the for loop is broken.
for ($i = 0; $i <= $totalCharacters; $i++) {
if ( $array1[$i] !== $array2[$i] ) {
$firstCharacter1 = $array1[$i];
$firstCharacter2 = $array2[$i];
$itsPosition = $i + 1; // adding 1 since the array's character count starts at zero (1 is easier for humans)
break;
} // end IF condition
} // end FOR loop
// echo out your results
if ( !isset($itsPosition) ) {
echo "Strings are identical.";
} else {
echo "The first difference occurs at character # " . $itsPosition . " of the strings.
It is " . $firstCharacter1 . " in the first string,
and " . $firstCharacter2 . " in the second string.";
}
?>
Give that a shot and let us know if it works for you.
Robert Walker
17,146 PointsThat works perfectly! though I would not + 1 to the position and leave it as a 0 index instead.
At least your version worked, nice bit of code to have actually though still struggling to see its uses.
Eric Butler
33,512 PointsYeah, normally I wouldn't do the + 1 either but I figured just for the sake of my echo statement it'd make more sense. Cheers Robert!
Eric Butler
33,512 PointsHm. I'm suddenly realizing that this might be used maliciously as a way to compare password hashes which (conveniently) are often all the same length. Hey Agon, can you write a comment back about what you're using this for to alleviate my concerns that we're helping you do something bad?
Agon Karakashi
822 PointsHello Eric,
Thanks for reply, I have not had the time to go through solutions that you gave, I will check later as I am at work.
Just to clear this up, I have no intentions to use it for any malicious reason. This was a question in a exam in University, and I had problems figuring it out how to work . I
Eric Butler
33,512 PointsAwesome, thanks Agon! Then yep, my code is what you need. You can mark this question as "Answered." Happy coding!