In the attached ideone link you have taken the input with spaces in between. The question mentions that we will take words as input. This means there will be no spaces in between 0s and 1s. Try removing the spaces and you will not get the desired output.
Thanks man i figured it out and solved it but now i am getting TLE so need to change my approch.