fixed bug in the length detection
removed trailing space
added more test cases and trimmed white spaces