I am trying to modify this function to read input in a noisy environment, meaning the characters received in the input may have extra characters.
A protocol that I am trying to implement is to send each digit twice in a row so that unless the receiver gets two of the same digit characters in a row, any other characters are ignored. It is assumed that there will be at least one integer per line.
For example, if the input is:
sw88zzx9cek44m, ,6ffe6qoi77#l85qaz22aaa
The output would be:
8472
Here is the code I have so far:
#include <stdio.h> #include
"tfdef.h"
#include
"chrutil.h"
/* Function reads the next integer from a noisy input */
int get_noisy_int()
{ int n = 0;
int got_dig = FALSE;
signed char ch;
ch = getchar();
while (IS_WHITE_SPACE(ch))
ch = getchar();
while (IS_DIGIT(ch))
{
n = n * 10 + dig_to_int(ch);
got_dig = TRUE;
ch = getchar();
}
if(ch == EOF) return EOF;
if(!got_dig) return ERROR;
return n;
Can anyone please give me some suggestions?