You'd be better off doing this with a higher level language with some more built in functions which would do the triming and sorting job for you, instead of going nuts with C...
I remember doing this with php / html (with an input form ) and it worked like a charm
