mobilef, I guess you're new to programming?, so perhaps I can give you some hints. I'm afraid that posting solutions to users problems is not the done thing on these forums, otherwise the user will miss out on the learning experience, but here's a little help.
Look up the statements "FUNCTION", "WHILE", "READLN", "LENGTH", and "IN" in a good text book. Using these statements together with some IF THEN statements, you can make a function that will do what you want.
Whenever you write a program, never assume the user will type in what you expect! Error checking makes software a little more complicated but a lot more robust.