Answer to life, the universe, and everything
Golfscript, 20
26,{.97+\{.}*}%=42`*
with new line, 21 chars (by Nabb)
26,{).[96+]*}%n+=42`*
Actually Nabb beat mine, here is original solution for with new line, 22 chars
26,{.97+\{.}*}%n+=42`*
This is simply generating source string and just comparing it against string from stdin.
Ruby 1.9, 46 42 39 characters
p (?a..?z).map{|a|a*$.+=1}*""==gets&&42
Assumes the input isn't terminated with a newline.
C program - 78 89
Edit: Do not print 42 when there are extra characters.
Assumes input does not have a trailing newline.
main(i,j){for(i=1;i<27;i++)for(j=i;j--;getchar()==96+i?0:exit(1));puts("42"+!!gets(&i));}
If the prefix does not match, the program exits. If the prefix matches but there is 1-3 or so extra characters, prints 2. Otherwise, produces undefined behavior.
This can be made one character shorter by changing exit(1)
to fork()
. Oh, and on an unrelated note, remember to save any open documents in case, for whatever reason, your system happens to lock up.