简体   繁体   中英

how to separate integers and operators from a string in c?

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?

PS I am using the code in my reader function which I got from here : How to extract numbers from string in c?

You can pass the integer array and operator array to the render() function as parameters like render( temp_0, arrNum, arrOperator, &numCount, &opCount) , where arrNum is an array of long and arrOperator is an array of char and the numCount and opCount are two integers which will denote the number of integers and operators respectively. These last two integer will be populated in render() . Then the modified render() function might look like -

void reader(char *temp_0, long *arri, char *arro, int *numCount, int *opCount){
char *p = temp_0;
int integerCount = 0;
int operatorCount = 0;

while(*p){
    if (isdigit(*p)){
        long val = strtol(p, &p, 10);
        arri[integerCount++] = val;
    }else{
       if((*p == '+') || (*p == '-') || 
          (*p == '/') || (*p == '*'))/* Add other operators here if you want*/
       {
          arro[operatorCount++] = *p;
       }
    p++;
    }
}

    *numCount = integerCount;
    *opCount  = operatorCount;

}

Note that there is no error checking done in the code. You might want to add that.

I wrote a sample test. Sorry for the tough code, since not have too much time. But it works well on my VS.

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

int main(){
    //Here I think By default this string is started with an integer.
    char *str = "65 + 9 - 4";
    char *ptr = str;
    char ch;
    char buff[32];
    int  valArray[32];
    int  val, len = 0, num = 0;
    while ((ch = *ptr++) != '\0'){
        if (isdigit(ch) && *ptr != '\0'){
            buff[len++] = ch;
        }
        else{
            if (len != 0){
                val = atoi(buff);
                printf("%d\n", val);
                valArray[num++] = val;
                memset(buff, 0, 32);
                len = 0;
            }
            else if (ch == ' ')
                continue;
            else
                printf("%c\n",ch);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM