Your code is not passing even sample test cases. See here, your way of taking input wasn’t correct.
Whenever you input a, there also a “\n” character left by cin. cin and cout flushes \n character in the input stream. Therefore whenever you use getsend of file indicator is set(feof). Hence, the pointer returned is a null pointer and the contents of b remain unchanged. Same is the case when you take input using getline(). Hence, use cin.