Hi !!!!!

I have to filter the input for a geniric string with some operations.
for example :

char buf [] ="25+2;44+1;8-2."

char op1[20];
char op2[20];
char operazione[20];
char en[20];//in this varible i put ";" for the next operation , I put "." becouse the operations are end
sscanf(buf,"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );

In this way I can filter 25+2; but I don't know how to filter the rest of the string.
I can't iterate for a generic string with a lot of operations.

Help me please also with other methods.

For this to work, you'll have to save the number of characters matched in each retrieval... Something like below.

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

int main(int argc, char**argv)
{
	int res = 0;
	int res2 = 0;
	char buf [] = "25+2;44+1;8-2.";

	char op1[20];
	char op2[20];
	char operazione[20];
	char en[20];

	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	res2 += res + 1;
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);
	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	res2 += res + 1;
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);
	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);

	exit(EXIT_SUCCESS);
}

I would put this functionality in a while loop to clean it up.

Oh thanks a lot Gerad !!!!
Can u show me the final code with the loop ?

Oh thanks a lot Gerad !!!!
Can u show me the final code with the loop ?

How about you show us the final code.

int res = 0;
	int res2 = 0;
	char buf [] = "25+2;44+1;8-2.";
 
	char op1[20];
	char op2[20];
	char operazione[20];
	char en[20];
	
	

		
		int i;
		int j=0;
		
		
		for(i=0;i<strlen(buf);i++){
		
		if(buf[i]==';'||buf[i]=='.')j++;
		
		} //COUNT THE NUMBER OF OPERATIONS
			
		int k;
			
		for(k=0;k<j;k++){
		res= sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
		printf("Scanf: %d \n",res);
		fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);
		res2+=res+1;
		
		}

Thanks Gerard!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.