tags:

views:

145

answers:

3

We are challenged to write a program that prompts the user to enter a password. The right password is " I Love Java programming " And it should be that the password has a single space before and after just like what I've written. Now, there is a condition, that if a user enters more than 2 or more spaces before or after the right password, the computer will still accept it!,,can u help me?

+6  A: 

What language?

Here's a language-agnostic answer to start with.

You have a few options here.

  • You can write a little "state machine" (which I here use very informally to describe some kind of iterator over all the characters in the password, that expects a certain pattern to the input), in which you check first for 1 or more spaces, then the string in the middle, then 1 or more spaces.
  • You can use a regular expression. Depending on your language, there might be built-in support for regular expressions-- go on Google and type " regular expressions" to find out more.
  • Many languages have string "trim" functions, which essentially get rid of leading or trailing junk characters such as whitespace. You can check the first and last characters of the string to make sure that they are whitespace, and then do a trim and check for "I Love Java programming" (no spaces). This guarantees that the password is correct in central content and surrounded by AT LEAST one space on each side.

Since this appears to be a homework question and I don't feel like writing code for you, I'm just going to leave you with this and you can figure out how to implement it in the language you need to use.

Either as you code or afterwords, make sure you take the time to figure out WHAT each of the methods I have proposed means (that is, HOW each algorithm works) and also WHY it (the algorithm) works. Being able to understand why will help you solve similar problems in the future and will also lead you to come up with your own algorithms for different classes of problems as you gain more experience.

Platinum Azure
Oh, and to other users of SO: Please don't knock me too heavily for using "state machine" in an informal (and likely technically incorrect) way. I'm using it in a very "intuitive" sense-- that is, sometimes it just helps me to use that particular term for this operation because it's just how I understand it best. That might hold true for others, too.
Platinum Azure
+1  A: 

Here's my smart aleck answer as a shell script:

#!/bin/sh
echo -n "Password: "
read given
if (echo "$given" | grep -x ' *I love shell scripting *' > /dev/null) ; then
    echo "Password accepted"
else
    echo "Access denied"
fi

To make it secure, you could hash the password:

#!/bin/bash
read -s -p "Password: " given
echo
hash=$(echo "$given" | sed 's/^ *//' | sed 's/ *$//' | sha1sum)
if [ "$hash" = "6ee322d4a87ec61912b363e704247a1dbad94975  -" ] ; then
        echo "Password accepted"
else
        echo "Access denied"
fi

My point is, in real life, storing passwords plaintext is pretty much always a bad idea (though for this assignment security certainly isn't an issue). This is a fairly simple example of how to make passwords more secure.

Joey Adams
So here we have a ccan GSOC student, answering a homework question .. with a shell script AND humor. I love it, +1.
Tim Post
A: 

If you are using Java try the String.trim() method.

Jorge Palacio