-2

I need to assign numbers in different variables (from string) and by using the letters do some operations on the numbers.

m -15 3 c 1 -15 1 11 m 2 2 r 30 m -11 40 l 60 m 1 5 d<br>


-every letter represents a action
-every number is the measurements
-user gives this input, and i don't know how to assign separately, or after assign the input as a string i couldn't separate.
(also couldn't find the right phrase to search)
I have tried like this:

while(input[i]!='\0'){
        switch(input[i]){
            i++;
            case 'm': a=input[i]*input[i+2];
                      printf("%d",a);
                      i+=6; // going for the next letter if i assume that
                           // user gave the input with single spaces between every integer or letter 
                      break;
            case 'c': ............................
                      i+=4;
                      printf("\n----CASE C");
                      break;

            default: printf("\nDEFAULT"); return 0;

        }

    }

something like this, but truth user might give without spaces or with many spaces and its not certain that how many input user will give, so is there any way to extract integers? (letters also important and integer can be negative)

Lkhagva Erdene
  • 135
  • 1
  • 2
  • 7
  • 1
    Did you try anything? – Oliver Charlesworth Apr 15 '17 at 19:03
  • yes i did while(input[i]!='\0'){i++ switch case : m }.... something like this but its uncertain that user might give negative numbers or 2 digit numbers my code will work only on 1 digit and positive numbers – Lkhagva Erdene Apr 15 '17 at 19:06
  • 1
    Please do not drip-feed information in comments, but edit the question to show the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) you have tried, and ask about a specific difficulty – Weather Vane Apr 15 '17 at 19:13
  • You'd only get our help if you show us any research done from your side. – Rohan Kumar Apr 15 '17 at 19:17
  • Possible duplicate of [How to extract numbers from string in c?](http://stackoverflow.com/questions/13399594/how-to-extract-numbers-from-string-in-c) – Gaurav Pathak Apr 15 '17 at 19:20
  • @GauravPathak thats not helping.... Its skipping letters i need those letters too!! and i also need to care about sign – Lkhagva Erdene Apr 15 '17 at 19:31
  • 1
    Although many commenters do not like `strtok` you could use it to extract each token from the input string. The first token string will be `"m"` and you can compare that (or its first character) with your actions. Having found `'m'` you will then extract two more tokens `"-15"` and `"3"` and obtain their numeric value with `sscanf`. And so on, once you are successful you can research the limitations of `strtok` and improve the solution. There is a basic example of the use of `strtok` [here](https://msdn.microsoft.com/en-us/library/2c8d19sb.aspx) – Weather Vane Apr 15 '17 at 19:38
  • thats good advice @WeatherVane, now how i can let sscanf start after the letter ? for example after "c" function should get 4 numbers, sscanf(input,%d%d%d%d) will take the integers that next to letter "m". – Lkhagva Erdene Apr 15 '17 at 19:43
  • Slow down and have a little think about how `strtok` works please. The first step will be to print every token extracted without trying to interpret them. Step by step. – Weather Vane Apr 15 '17 at 19:44
  • YEAAA THANK YOU @WeatherVane i just learned that strtok which is one of the most important function (at least im gonna use it a looot)... i figured my problem with your help thanks again :) – Lkhagva Erdene Apr 15 '17 at 21:38

1 Answers1

1

Use sscanf() to find patterns

"%n" records the number of characters scanned.

sscanf(&input[i], " m%d%d %n", &op[0], &op[1], &n) will return 2 if the string matches:

optional-space `m` optional-space integer optional-space integer optional-space   

and the sets n to the number of character scanned.

#include <stdio.h>
int main() {
  char input[] = "m -15 3 c 1 -15 1 11 m 2 2 r 30 m -11 40 l 60 m 1 5 d\n";

  int op[4];
  int i = 0;

  while (input[i]) {
    int n = 0;
    if (sscanf(&input[i], " m%d%d %n", &op[0], &op[1], &n) == 2) {
      printf("CASE m: %d\n", op[0] * op[1]);
    } else if (sscanf(&input[i], " c%d%d%d%d %n", &op[0], &op[1], &op[2], &op[3], &n) ==4){
      printf("CASE c\n");
    } else if (sscanf(&input[i], " r%d %n", &op[0], &n) == 1) {
      printf("CASE r\n");
    } else if (sscanf(&input[i], " l%d %n", &op[0], &n) == 1) {
      printf("CASE l\n");
    } else if (sscanf(&input[i], " d %n", &n), n) {
      printf("CASE d\n");
    } else {
      printf("?\n");
      n = 1;
    }
    i += n;  // advance to the next unprocessed char
  }
}

Output

CASE m: -45
CASE c
CASE m: 4
CASE r
CASE m: -440
CASE l
CASE m: 5
CASE d

This assumes each action uses a fixed number of operands. Additional code could handle variant operand count.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256