code is here : https://ideone.com/9quXGW