Question

I have a bunch of strings structured like this one

Trim(2714,8256)++Trim(10056,26448)++Trim(28248,49165)

and what I want to do is to save all the numbers into an array (for the sake of this answer let's say I want to save the numbers of just one string).

My plan was to find the the position of the first digit of every number and just read the number with sscanf, but as much as I've thought about it, I couldn't find a proper way to do so. I've read a lot about strstr, but it is used to search for a string into another string, so I should search for the exact number or do 10 cases to cover from 0 to 9.

Thanks in advance for your support!

Was it helpful?

Solution

You could try something like this:

  • Walk the string until you find the first digit (use isdigit)
  • Use strtoul to extract the number starting at that position
    • strtoul returns the number
    • the second argument (endptr) points to the next character in the string, following the extracted number
  • Rinse, repeat

Alternatively you could tokenize the string (using "(,+)") and try to strtoul everything.

OTHER TIPS

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

int main() {
  int arr[10], idx=0, d, l=0;
  char *p, *str = "Trim(2714,8256)++Trim(10056,26448)++Trim(28248,49165)";
  for (p = str; *p != 0; p+=l) {
    l = 1;
    if (isdigit(*p)){
      sscanf(p, "%d%n", &d, &l);
      arr[idx++] = d;
    }   
  }   
  for (l=0; l<idx; l++) {
    printf("%d\n", arr[l]);
  }
  return 0;
}

You can also try using YACC or Lex, which will format your string as you want.

Here is how I would think about the code:

  • start loop over source array characters
    • if the character in the current position (of the source array) is a digit
    • copy it to the destination array (in the current position of the destination array)
    • move to the next position in the destination array
    • move to the next position in the source array
    • if the end of the source string is reached, exit loop
  • make sure that the destination string is terminated properly (i.e. by '\0')

Note that we are counting with two different counters one for the source array which will increment with every loop iteration and the other for the destination array and will only increment if a digit is found

checking of a character is a digit or not can be done using the function "isdigit()" but it will require the header file ctype.h

Another way to check if the character is a digit is by checking its value in reference to the ASCII table character '0' equals 48 and character '9' equals 57. So if the character is within that range it is a digit, other wise it is a character. You can actually compare directly with the characters.

if (character >= '0' && character =< '9') printf("%c is a digit", character);
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main() {
  int array[8], count=0, data;
  const char *s = "Trim(+2714,8256)++Trim(10056,26448)++Trim(28248,49165)";
  char *p;
  while(*s) {
    if(isdigit(*s) || *s=='-' && isdigit(s[1])){
      data = strtol(s, &p, 10);
      s = p;
      array[count++] = data;
    } else
        ++s;
  }
  {//test print
    int i;
    for(i=0;i<count;++i)
      printf("%d\n", array[i]);
  }
  return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top