P.S.
P.S.

Reputation: 384

What is the most efficient way to find numbers within a string of other characters?

String of 3 numbers of any length - embedded in spaces and tabs, represented below with the \t marker:

123456789 \t 12345 \t6789\n

There can be any amount and combination of space and tab' between the numbers.

For example, this is also valid:

1    \t \t2\t\t\t\t\t \n3\n  

What is the most efficient way to extract the 3 numbers ?

Looking at sscanf() but seems the exact format of the string is needed, is that correct?

Thank you.

Upvotes: 0

Views: 84

Answers (2)

johnnycrash
johnnycrash

Reputation: 5344

sscanf() is really slow. In ascii, tabs and spaces are all < '0', so you can do this:

char* sz = "123456789 \t 12345 \t6789\n"
char* aNum[10] = { 0 };
int cNum = 0;
for (int i=0 ; sz[i] ; ++i) {
  if (sz[i] < '0')
    sz[i] = 0, cNum++;
  else if (!aNum[cNum])
      aNum[cNum] = sz + i;
}

You could make this more efficient with more loops

char* sz = "123456789 \t 12345 \t6789\n"
char* aNum[10] = { 0 };
int cNum = 0;
for (int i=0 ; sz[i] ; ++i) {
  if (sz[i] >= '0') {
      aNum[cNum++] = sz + i;
      for ( ; sz[i]>='0' ; ++i) { }
      sz[i] = 0;
  }
}

Upvotes: 1

Chris Dodd
Chris Dodd

Reputation: 126408

The most obvious/simplest is sscanf:

if (sscanf(buffer, "%d%d%d", &v1, &v2, &v3) == 3) {
    /* extracted 3 numbers successfully */

The %d format specifier skips whitespace (which include \t and \n characters), so the variable extra stuff doesn't matter. It will also skip spaces, which may or may not be what you want.

Upvotes: 5

Related Questions