Instead of accessing buf and bufp, it would be
easier to use ungetch
since a lot of the code in it is
common with our new function. Keeping in mind that stacks work on the
last in, first out principle, we want to push back the last character
first, then the second-to-last character, and so on until the entire
string has been pushed into buf. We achieve this by
incrementing i until it equals the index of the last
character of s, at which point we push back each character
in a for
-loop that decrements i until it
reaches zero.
/* ungets: push character string back on input */
void ungets(char s[])
{
int i;
for (i = 0; s[i] != '\0'; ++i)
;
--i; /* exclude \0 */
for (; i >= 0; --i)
ungetch(s[i]);
}