|
Post by echnaret on Sept 18, 2011 19:42:29 GMT
I saw a thread like this on another message board a while back, and I really liked the idea behind it. Basically, every programmer has a slightly different way of going about things. One way you can see this is by examining their source code.
For this thread, make a simple program in whatever language you want. The program will take in a string of user's input, and then repeat it back, only backwards.
I'll try to post an example of my own in a few days.
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on Sept 18, 2011 21:31:00 GMT
I'll try to post an example of my own in a few days. A few days? This should only take about 60-120 seconds. C++ #include <iostream> #include <string>
using namespace std;
int main() {
string message;
cout << "Enter a message: "; getline(cin, message);
int length = message.length(); for(int front = 0, back = length-1; front < back; front++, back--) { char temp = message[front]; message[front] = message[back]; message[back] = temp; }
cout << message << endl;
return 0; }Java package lawl;
import java.util.Scanner;
public class ReverseString { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter sentence:"); String message = scanner.nextLine(); StringBuffer reverseMessage = new StringBuffer(); int length = message.length(); for(int back = length-1; back > -1; back--) { reverseMessage.append(message.charAt(back)); } System.out.println(reverseMessage); } }Javascript String.prototype.reverseStr = function() { var text = new String;
for(a = this.length-1; a > -1; a--) { text += this.charAt(a); }
return text; }
|
|
|
Post by echnaret on Sept 19, 2011 2:10:08 GMT
Well, I was wanting to see what other people came up with before I posted my own.
Quick and dirty PHP:
<?php
if (isset($_POST['submit']) && strlen($_POST['user_input']) > 0) { echo "<p>Your reversed text is: ".strrev($_POST['user_input'])."</p>"; } else { echo "<p>Please enter some text into the box below, and it will be reversed for you!</p>"; }
echo "<form method=\"POST\">" ."<input name=\"user_input\">" ."<input type=\"submit\" name=\"submit\" value=\"Reverse!\">" ."</form>";
?>
|
|
|
Post by echnaret on Sept 19, 2011 2:41:35 GMT
I also created an OOP version of this code. This probably isn't the greatest example, as it's really not needed. However, I felt like doing it for the heck of it. Here is the result:
<?php
/** * This class contains the program logic. It takes the * user's input, reverses it, and then sanitizes it. It * also creates a Printer object, which prints the page. */ Class Controller { public function __construct() { if (isset($_POST['submit']) && strlen($_POST['user_input']) > 0) { // Reverse the string $reversed_sting = strrev($_POST['user_input']);
// Sanitize the user input so they can't mess with the page formatting. // See http://php.net/manual/en/function.filter-var.php for more // details.
// NOTE: The string must be reversed, and then sanitized. Otherwise, // the character encodings will also be reversed, and the resulting // string will look garbled. $sanitized_reversed_input = filter_var($reversed_sting, FILTER_SANITIZE_SPECIAL_CHARS); // Create the message $message = "Your reversed text is: ".$sanitized_reversed_input;
// Sanitize the user input that will be displayed in the text box $sanitized_user_input = filter_var($_POST['user_input'], FILTER_SANITIZE_SPECIAL_CHARS);
// Print it! new Printer($message, $sanitized_user_input); } else { $message = "Please enter some text into the box below, and it will be reversed for you!"; new Printer($message); } } } /** * This class is used to print out what * the user actually sees on the page. */ Class Printer { public function __construct($message, $user_input="") { $this->print_header(); $this->print_message($message); $this->print_form($user_input); $this->print_footer(); }
private function print_header() { echo "<html>\n" ."<head><title>Text Reverser</title></head>\n" ."<body>\n"; }
private function print_message($message) { echo "<p>".$message."</p>"; }
private function print_form($user_input) { echo "<form method=\"POST\">" ."<input name=\"user_input\" value=\"".$user_input."\">" ."<input type=\"submit\" name=\"submit\" value=\"Reverse!\">" ."</form>"; }
private function print_footer() { echo "</body>\n" ."</html>\n"; } }
new Controller();
?>
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on Sept 19, 2011 3:01:05 GMT
Well, I was wanting to see what other people came up with before I posted my own. Ah, my bad. I thought you meant you were going to spend a few days on it so I was wondering if you meant to code something else. And I'm surprised PHP has the method built in, I wouldn't think it would be used very often. By the way, is it now standard to use __construct rather than the class' name for the constructor? The languages is being updated a lot and I don't use it enough to need to keep up with the changes.
|
|
|
Post by echnaret on Sept 19, 2011 3:04:14 GMT
Ah, my bad. I thought you meant you were going to spend a few days on it so I was wondering if you meant to code something else. And I'm surprised PHP has the method built in, I wouldn't think it would be used very often. No worries. Yeah, I've actually found that PHP has a lot of random methods that you probably wouldn't expect. For instance, one I've found useful is ucfirst(). That function capitalizes the first character in a string, if it is alphabetic.
|
|
prads
Elite Poster
[M:0]
It's a shame that PI isn't an integer :(
Posts: 361
|
Post by prads on Sept 19, 2011 3:43:33 GMT
I'll try to post an example of my own in a few days. C++ #include <iostream> #include <string>
using namespace std;
int main() {
string message;
cout << "Enter a message: "; getline(cin, message);
int length = message.length(); for(int front = 0, back = length-1; front < back; front++, back--) { char temp = message[front]; message[front] = message[back]; message[back] = temp; }
cout << message << endl;
return 0; }I would like to make a small change in Jordan's C++ code. You can use XOR operation to swap numbers so you don't have to create an extra temp variable, like this: for(int front = 0, back = length-1; front < back; front++, back--) { message[front] = message[back] ^ message[front]; message[back] = message[back] ^ message[front]; message[front] = message[back] ^ message[front]; }
|
|
xcessive
Epic Poster
.[M:5000]
Posts: 526
|
Post by xcessive on Sept 19, 2011 8:02:22 GMT
I always try not to use arrays when I can:
#include <stdio.h> #include <stack> int main() { std::stack<char> s; while(s.empty() || s.top() != EOF) s.push(getchar()); while(!s.empty()){ putchar(s.top()); s.pop(); } return 0; }
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on Sept 19, 2011 15:37:06 GMT
I would like to make a small change in Jordan's C++ code. You can use XOR operation to swap numbers so you don't have to create an extra temp variable, like this: for(int front = 0, back = length-1; front < back; front++, back--) { message[front] = message[back] ^ message[front]; message[back] = message[back] ^ message[front]; message[front] = message[back] ^ message[front]; } Very nice, I remember using that algorithm in my assembly class. I always have to write those out on paper to see how they work. Front = 0001 Back = 0011 Front = 0011 ^ 0001 = 0010 Back = 0011 ^ 0010 = 0001Front = 0001 ^ 0010 = 0011I would have never thought of that on my own. I always try not to use arrays when I can: #include <stdio.h> #include <stack> int main() { std::stack<char> s; while(s.empty() || s.top() != EOF) s.push(getchar()); while(!s.empty()){ putchar(s.top()); s.pop(); } return 0; }
Why do you prefer to not use arrays?
|
|
xcessive
Epic Poster
.[M:5000]
Posts: 526
|
Post by xcessive on Sept 20, 2011 0:38:16 GMT
Because it makes things more interesting, so mainly just for fun. Although a stack is based on a dequeue from memory which is based on arrays haha.
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on Sept 20, 2011 3:28:23 GMT
lol if you want to have some fun you should write one that randomly sorts the letters then checks to see if they match the string backwards, and, if not, randomly sorts them again, until the string is perfectly reversed. A possible running time of infinity is always fun. And my name turned purple on this page load, wut.
|
|
xcessive
Epic Poster
.[M:5000]
Posts: 526
|
Post by xcessive on Sept 20, 2011 8:45:44 GMT
If it doesnt work, just use brute force .
|
|