Reputation: 372
I am trying to take each characters in a sentence and change them to a different one to "encode" the sentence (More like just make it unreadable).
The idea is to take this string : "abc" and turn each characters in it into the next one in the alphabet, giving "bcd". So on an 'A' becomes a 'B' and a 'Z' becomes an 'A' when you reach the end. I created this code which indeed works, but only if you're trying to turn a 'B' into an 'A' which is going backwards in the alphabet.
Here is the code that works (turns B's into A's) :
<?php
$SentenceToEncode="hello stackoverflow, this is my first question. thanks for helping!";
$Alphabet="abcdefghijklmnopqrstuvwxyz";
$ModifiedAlphabet="zabcdefghijklmnopqrstuvwxy";
$SentenceLength=strlen($SentenceToEncode);
for($i=0;$i<$SentenceLength;$i++){
for($j=0;$j<26;$j++){
if($SentenceToEncode[$i]===$Alphabet[$j]){
$SentenceToEncode[$i]=$ModifiedAlphabet[$j];
}
}
}
echo $SentenceToEncode;
?>
Which results in this when I run it :
gdkkn rsybjnudqeknv, sghr hr lx ehqrs ptdrshnm. sgymjr enq gdkohmf!
But when I try this second block that turns A's into B's (which is what I want) :
<?php
$SentenceToEncode="hello stackoverflow, this is my first question. thanks for helping!";
$Alphabet="abcdefghijklmnopqrstuvwxyz";
$ModifiedAlphabet="bcdefghijklmnopqrstuvwxyza";
$SentenceLength=strlen($SentenceToEncode);
for($i=0;$i<$SentenceLength;$i++){
for($j=0;$j<26;$j++){
if($SentenceToEncode[$i]===$Alphabet[$j]){
$SentenceToEncode[$i]=$ModifiedAlphabet[$j];
}
}
}
echo $SentenceToEncode;
?>
I get this :
aaaaa aaaaaaaaaaaaa, aaaa aa aa aaaaa aaaaaaaa. aaaaaa aaa aaaaaaa!
Where did I go wrong? The only difference being the modified alphabet's first and last two letters.
Thanks in advance!
Upvotes: 1
Views: 248
Reputation: 9
Try this. You need to execute the second loop as soon it matches the alphabet.
<?php
$SentenceToEncode="hello stackoverflow, this is my first question. thanks for helping!";
$Alphabet="abcdefghijklmnopqrstuvwxyz";
//$ModifiedAlphabet="zabcdefghijklmnopqrstuvwxy";
$ModifiedAlphabet="bcdefghijklmnopqrstuvwxyza";
$SentenceLength=strlen($SentenceToEncode);
for($i=0;$i<$SentenceLength;$i++){
for($j=0;$j<strlen($ModifiedAlphabet);$j++){
if($SentenceToEncode[$i]===$Alphabet[$j]){
$SentenceToEncode[$i]=$ModifiedAlphabet[$j];
break;
}
}
}
echo $SentenceToEncode;
?>
Upvotes: 0
Reputation: 59691
You have to add a break;
in your if statement (I would do it for both scripts to save some iterations)! Like this:
if($SentenceToEncode[$i]===$Alphabet[$j]) {
$SentenceToEncode[$i]=$ModifiedAlphabet[$j];
break;
}
Why?
Because if you don't do this it runs the inner loop 26 times for every character! So what that means is:
It first searches the letter h
in the normal alphabet and it will find it at position 7. So now it's going to replace the first letter with the letter in the modified alphabet at position 7 which is i
. BUT now you don't break the inner loop and it's going for the next iteration to search the first letter, which is now i
in the normal alphabet and i
is exactly the next letter in the inner loop.
So you replace every character to the end of the modified alphabet! (This didn't happened to you in the first time, because you moved it backwards so it couldn't find it again)
Upvotes: 2
Reputation: 449
You can do it much easier using str_replace function.
$decoded = array("a", "b", "c", ...., "z");
$encoded = array("z", "a", "b", ...., "y");
$encoded_text = str_replace($decoded, $encoded, $original_text);
$decoded_text = str_replace($encoded, $decoded, $encoded_text);
You can also create initial arrays from strings using str_split function
Upvotes: 2