Implement a custom string manipulation algorithm that handles edge cases such as trailing spaces and special characters.