Jump to content

Caeser cipher help - excluding non-alphabetic characters


winthropite

Recommended Posts

Hey everyone,

I wrote a Caesar cipher (a script where, given a message, the letters in the message are shifted x number of places; e.g. A => D, B => E, C => F, X => A, Y => B, Z => C, etc.). It works but it shifts all ASCII characters. I want to exclude characters that are not A-Z or a-z (outside the ASCII values of 65 - 90 and/or 97 - 122) so that spaces, punctuation, numbers all remain the same instead of shifting as well.

Here's the form on index.html

<form class="form-inline" role="form" method="post">
						<div class="table-responsive">
						<table>
							<tr class="tr_top">
								<td class="td_top"><textarea class="form-control" rows="4" name="msg" placeholder="Your message here." onfocus='this.select()'><?php
										require ('encode.php');
										require ('decode.php');

										if (isset($_POST['encode'])) {
											echo $encstring;
										} elseif (isset($_POST['decode'])) {
											echo $decstring;
										}
									?></textarea></td>
							</tr>
							<tr class="tr_mid">
								<td class="td_mid"><input type=text class="form-control input_mid" name="offset" value="<?php if (isset($_POST['encode']) || isset($_POST['decode'])) { echo htmlspecialchars($_POST['offset']);} ?>" placeholder="Enter a number." pattern="[0-9]{0,3}" oninvalid="setCustomValidity('Please enter a number between 1 and 999.')" oninput="setCustomValidity('')"></td>
							</tr>
							<tr class="tr_bottom">
								<td class="td_bottom">
									<input class="input_bottom btn btn-default submit" type="submit" name="encode" value="Encode">
									<input class="input_bottom btn btn-default submit" type="submit" name="decode" value="Decode">
									<input class="input_bottom btn btn-default" type="button" value="Clear"</td>
							</tr>
						</table>
						</div><!-- close table-responsive -->
					</form>

				<?php
				//encode
				require ('encode.php');
				if (isset($_POST['encode'])) {
					echo "<p>Original message:</p>";
					echo "<p class='string ital'>" . $string . "</p>";
					echo "<p>Encoded message:</p>";
					echo "<p class='string ital'>" . $newstring . "</p>";
				}

				//decode
				require ('decode.php');
				if (isset($_POST['decode'])) {
					echo "<p>Encoded message:</p>";
					echo "<p class='string ital'>" . $string . "</p>";
					echo "<p>Decoded message:</p>";
					echo "<p class='string ital'>" . $newstring . "</p>";
				}
				?>

and here is encode.php

<?php
$string = $_POST['msg'];
$newstring = $string;
$sp = $_POST['offset'];

for ($i=0; $i < strlen($string); $i++) {
	$ascii = ord($string[$i]);
	for ($j=0; $j < $sp; $j++) {
		if ($ascii == 90) { //uppercase bound
			$ascii = 65; //reset back to 'A' 
		} 
		else if ($ascii == 122) { //lowercase bound
			$ascii = 97; //reset back to 'a' 
		} 
		else {
			$ascii++;
		}
	}
	$newstring[$i] = chr($ascii);
	$encstring = $newstring;
}
?>

Any help would be appreciated as I can't figure it out. If you want to see it in action it's here.

Link to comment
Share on other sites

Yes, it would be bad if you asked to be spoon fed the solution. Especially since this seems to be a homework problem.

Yes, it was. And I have code that works; I was the only student who did. I was also the only one who put the code into a fully responsive site. I passed the homework with flying colors. But far be it to assume you know the details; the class project is long over. I'd be glad to send you my instructor's email for confirmation. I'm finishing this for myself. So, that being said, is it possible to get a hand with this?

Edited by winthropite
Link to comment
Share on other sites

ctype_alpha evaluates the value you pass to it and returns true if it's all letters, false if not. If you really wrote all that code you posted, surely you can figure out how to incorporate a simple condition into it, especially since you use conditions elsewhere..

I did write it. I also tried multiple solutions, such as

<?php

$string = $_POST['msg'];

$newstring = $string;

$sp = $_POST['offset'];



for ($i=0; $i < strlen($string); $i++) {
   $ascii = ord($string[$i]);
for ($j=0; $j < $sp; $j++) {
   if ($ascii == 90) { //uppercase bound

      $ascii = 65; //reset back to 'A'
   } else if ($ascii == 122) { //lowercase bound
      $ascii = 97; //reset back to 'a'
   } else if ( ($ascii >= 65 && $ascii <= 90) || ($ascii >= 97 || $ascii <= 122 ) ) {
      $ascii++;
   }

}

$newstring[$i] = chr($ascii);
$encstring = $newstring;
}
?>

and

 

<?php

$sp = $_POST['offset'];
$string = $_POST['msg'];
$newstring = array();
for ($i=0; $i < strlen($string); $i++) {
    $ascii = ord($string[$i]);
    if ($ascii < 65 || ($ascii > 90 && $ascii < 97) || $ascii > 122) {
        $newstring[$i] = $string[$i];
    }
    else {
        for ($j=0; $j < $sp; $j++) {
            $ascii += 1;
            if ($ascii == 91 || $ascii == 123) {
                $ascii -= 26;
            }
        }
        $newstring[$i] = chr($ascii);
    }
}
$encstring = implode('', $newstring);
echo $encstring;

I'm honestly just looking for a hand. I'm new to PHP and am simply asking for help.

Edited by winthropite
Link to comment
Share on other sites

// receive user input 

// is user input valid?
if (ctype_alpha($string)) {
  // it's valid, do your caesar thing with the input.
} else {
  // it's invalid, tell user it's invalid input.
}
"Help" involves us pointing you in the right direction, which I did. You aren't asking for help. You are telling us what you want the code to do, and then asking for someone to post the finished code according to your specs. That's not asking for help. That's asking for someone to do it for you.

 

Look, I don't know you, but I do know that if you really did write that code above, there's no reason you shouldn't be able to do this yourself. So, either you are being lazy or you're lying about writing that code.

 

I have helped you by pointing you in the right direction. But I am not going to alter your script for you.

Link to comment
Share on other sites

I'm telling you what the problem with the code is and what I've tried to fix it. I've never heard of ctype alpha. I have been learning PHP for maybe 3 months now, 2 days a week. My feet are barely wet. I thought I showed how I did try myself but that's not enough. I've spent probably 20 hours messing with this code to get it to work right. Who the hell are you to assume I'm lazy or lying?

 

I expected help here but instead got hard-headedness now from you and outright rudeness from Kevin in the way he smugly made an assumption that was completely wrong instead of asking first. And now you assuming laziness or lies. Well, we all know what people who assume are, don't we?

 

There are a number of other similar forums where the members actually help and share code snippets to people in need. No assumptions, no rudeness, just people who understand that writing a bit of code to help out people who have already tried everything they can to get their code to work isn't perpetuating laziness; it's just common decency.

 

I'll figure this out on my own. I'd rather rewrite the code another 20 hours than continue to deal with the nasty, elitist attitudes here.

Link to comment
Share on other sites

Who the hell are you to assume I'm lazy or lying?

Who the hell are you that we should simply believe you?

 

I'd rather rewrite the code another 20 hours than continue to deal with the nasty, elitist attitudes here.

Good. If our elitist attitudes make you a better programmer, then I applaud us in general and .josh specific.

 

u8awa.gif

Edited by ignace
Link to comment
Share on other sites

winthropite, here is why I think you're full of shit.  You claim to have written that code yourself, which already has conditions and makes use of other php functions in it.  And yet you can't understand how to incorporate one simple little php function into a condition.  I told you what ctype_alpha does. I even gave you a link to the manual.  

 

To me, this is like someone who claims to know calculus and posted this big long complicated formula, and yet doesn't understand how to literally add 1+1 to it, even though that exact principle is shown in the formula they claimed to write.  Or, claiming you built a car and yet don't know how to change a tire.  Or, claiming you built a house and yet don't know how to put a lock on a door.  Do you understand what I'm saying here? Or claiming to build radio from scratch but not know how to add a volume dial.    I'm saying it is impossible for you to not know how to apply ctype_alpha to your code, if that is really your code that you wrote and fully understand, because the skill required to accomplish the code you posted demonstrates a greater comprehension of php than solving your issue necessitates.

 

So here I am scratching my head wondering how it is possible that you can write that code and not understand what to do with ctype_alpha, and there's only 2 logical reasons: you're being lazy, or you didn't write that code. Now, you can make remarks about people being "unhelpful" or being on high horses all you want. I really don't care. We are here to help people, and we did provide help.  I even showed you pseudocode showing the general structure for how to incorporate it into your code. But we are not here to do your work for you.  And you will find that same attitude on any other free help forum you come across.  

 

Yes, you showed what you tried, before any help was given.  But you have not shown what you have tried since help was given.  All you've done is ask for someone to do it for you. 

 

Show me how you tried to incorporate ctype_alpha into your code.  Where's that code?

Link to comment
Share on other sites

Yes, it was. And I have code that works; I was the only student who did. I was also the only one who put the code into a fully responsive site. I passed the homework with flying colors. But far be it to assume you know the details; the class project is long over. I'd be glad to send you my instructor's email for confirmation. I'm finishing this for myself. So, that being said, is it possible to get a hand with this?

 

You wrote a fully responsive site, yet you can't follow the link provided about ctype_alpha and read the accompanying documentation (which has relevant and clear examples of how to use it)?

 

...you do realize why everyone's bullshit detector is going off, right?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.