Ok..this problem is kind of confusing because I have to use these 2 functions getc() and ungetc().

What i'm supposed to do is take in input in the form of a string, find all digits, store the digits in the string, convert the string into a digit, and finally print it out. For instance if I have the string "be22again," the number 22 would be taken out to be processed.

The trouble that i'm having is with the ungetc() function. I'm not entirely sure about what it does nor do I really know how to implement it due to the lack of good examples. One website said it:

"The function to unread a character is called ungetc, because it reverses the action of getc"

so far this is what i've come up with (its compilable):

// 1. Read input with getc()
// 2. Check the string for non-digits
// 3. Use ungetc() to unread the non-digits
// 4. Store the digits into a string
// 5. Convert the string into a numerical value with atoi()
// 6. Display the conversion

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define SIZE 100

int main(void)
{
	char num_array[SIZE];
	int ch, cnt = 0;
	
	puts("Enter a string: ");
	
	while ((ch = getc(stdin)) && isdigit(ch))
	{
		if (isalpha(ch))
			ungetc(ch, stdin);
		num_array[cnt] = ch;
		cnt++;
	}

	printf("%d\n", atoi(num_array));

	return 0;
}

There's no reason to use ungetc in that program. Can you be more specific about the question, because it sounds like information is being lost in translation.

Suppose I have to use ungetc() in the program. Here i'll just post teh original instructions from this book:

Write a function that skips over input until encountering a digit. It then stores that digit and subsequent digits ina string until encountering a nondigit. The non digit is placed back in the input, and the function converts the digit string to a numeric value. The function should use a pointer argument to provide the numeric value to the calling program. Use the function return value to return EOF if the function encounters end of file; have it return 1 otherwise. Use getc() and ungetc(). In short, this function finds the next integer in output, whether it be isolated or embedded in text, as in be22again.

Ah, that makes much more sense. ungetc is used to push the first non-digit character after a digit back onto the stream so that other input functions won't lose the first character. For example, if you didn't push the 'a' back on after 22 in "be22again", the next string input would read "gain", and that introduces a lot of interesting misinterpretation issues. ;)

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ( void )
{
  int i = 0;
  char s[BUFSIZ];
  int ch;

  while ( ( ch = getc ( stdin ) ) != EOF ) {
    if ( isdigit ( ch ) )
      break;
  }

  if ( !feof ( stdin ) ) {
    s[i++] = ch;

    while ( ( ch = getc ( stdin ) ) != EOF ) {
      if ( !isdigit ( ch ) )
        break;
      s[i++] = ch;
    }

    ungetc ( ch, stdin );
    s[i] = '\0';

    printf ( "The number is %d\n", atoi ( s ) );

    if ( fgets ( s, sizeof s, stdin ) != NULL ) {
      s[strcspn ( s, "\n" )] = '\0';
      printf ( "The rest of the stream contains \"%s\"\n", s );
    }
  }

  return 0;
}
This article has been dead for over six months. Start a new discussion instead.