I want to make a parser and first step I have in mind is to extract integers and operators from an input string and store them in their respective arrays. What I have so far is this...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* Grammar for simple arithmetic expression
E = E + T | E - T | T
T = T * F | T / F | F
F = (E)
Legend:
E -> expression
T -> term
F -> factor
*/
void reader(char *temp_0){
char *p = temp_0;
while(*p){
if (isdigit(*p)){
long val = strtol(p, &p, 10);
printf("%ld\n",val);
}else{
p++;
}
}
}
int main(){
char expr[20], temp_0[20];
printf("Type an arithmetic expression \n");
gets(expr);
strcpy(temp_0, expr);
reader( temp_0 );
return 0;
}
Say I have an input of "65 + 9 - 4" and I want to store the integers 65, 9, 4 to an integer array and the operators +, - in an operators array and also ignores the whitespaces in the input. How should I do it?
P.S. I am using the code in my reader function which I got from here :How to extract numbers from string in c?