C Programming: How to read input byte at a time and output only integers as tokens? -
i'm new c programming , have write program picks integers standard input , output them tokens. else should outputted "illegal". i'm not permitted use arrays or malloc, , declare ints or long ints. must use getchar() input , printf() output , nothing else. question is, how read input byte @ time, convert them tokens , check if ints?
for example: if input is:
hello 45 world thank 67 it should output:
illegal 45 illegal illegal 67 illegal
#include <stdio.h> #include <ctype.h> #include <limits.h> int main() { int ch; int n; int takenum, sign; long long int wk;//long long int int64 wk=0ll; takenum = 0;//flag sign = 1;//minus:-1, other:1 while(eof!=(ch=getchar())){ if(ch == '-'){ sign = -1; continue; } if(ch >= '0' && ch <= '9'){ if(takenum >= 0) takenum = 1; else continue; wk = wk * 10 + (ch - '0')*sign; if(int_max < wk || int_min > wk){//overflow takenum = -1;//for skip } continue; } //space character continuing "illegal" if(ch == ' ' || ch == '\t' || ch == '\n'){ if(takenum <= 0) printf("illegal\n"); else printf("%d\n", n=wk); wk=0ll; takenum=0; sign=1;//reset } } return 0; }
Comments
Post a Comment