DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   extract multidigits from a char* (substring) (http://www.daniweb.com/forums/thread201053.html)

monkey_king Jul 1st, 2009 7:03 pm
extract multidigits from a char* (substring)
 
Hi,
given a cstring, I need to extract the digits in it, the digits are prefixed with either a '+' or '-'. Like
,.,.,.,+3ACT,.,.,.,.-12,.,.,.,.,.,.,.,actgncgt
#OUTPUT
3
12

I've made a working program that does what I want,
but it seems overly complicated.
Does anyone have an idea if this can be done smarter, better, faster?

Thanks in advance

Btw I checked the program with valgrind and there are no leaks or errors

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

int main(){
  char tmp_array[100];
  const char* seq = "+1236,,..,,actgn+3ACT-4CCCC";
  printf("%s\n",seq);

  for(int i=0;i<strlen(seq);i++){
    if(seq[i]!='+'&&seq[i]!='-')
      continue;
    int j=i+1;
    while(j<strlen(seq)){
      if(seq[j]>='0' &&seq[j]<='9'){
        j++;
      }else
        break;
     
    }
    strncpy(tmp_array,seq+i+1,j-i-1);
    tmp_array[j-i-1]='\0';
    printf("numbers in substrings are: %d\n",atoi(tmp_array));
  }
 
  return 0;
}

jephthah Jul 1st, 2009 7:26 pm
Re: extract multidigits from a char* (substring)
 
Quote:

Does anyone have an idea if this can be done smarter, better, faster?
check out: strtok() ... this breaks a string into tokens at each of one or more delimiters.

use it to break the string at each '+' or '-' (the delimiters), then check if the first one or more characters of each resulting string (the tokens) are numerals.

if it does, then push that number onto your result array. if it doesn't, skip on to the next token.




.

wildgoose Jul 1st, 2009 7:27 pm
Re: extract multidigits from a char* (substring)
 
untested but here...

int main()
{
  char tmp_array[100];
  const char* seq = "+1236,,..,,actgn+3ACT-4CCCC";
  printf("%s\n",seq);

  while (*seq)
  {        // pre-scan for +-
      if (*seq != '-' && *seq != '+')
      {
        seq++;
        continue;
      }

      char *p = tmparray;
          *p++ = *seq++;

      while (*seq)
          {
              if ((*seq < '0') || ('9' < *seq))
                  {
                    break;
              }

          *p++ = *seq++;
          }

      *p = 0;

      printf("numbers in substrings are: %d\n",atoi(tmp_array));
  }

  return 0;
}

wildgoose Jul 1st, 2009 7:31 pm
Re: extract multidigits from a char* (substring)
 
But if allowed to use off the shelf library functions, then poster jephthah has the right idea!

MrNoob Jul 1st, 2009 7:40 pm
Re: extract multidigits from a char* (substring)
 
why dont you put an array with stuff you want and extract that to new array?

wildgoose Jul 1st, 2009 7:42 pm
Re: extract multidigits from a char* (substring)
 
Double the processing time. No optimal!

Why process a list twice when you can do it once!

jephthah Jul 1st, 2009 7:54 pm
Re: extract multidigits from a char* (substring)
 
wildgoose: posting "Untested" code on daniweb's C forum is generally risky business :P

anyhow, being able to use strtok() correctly is tricky for a new user. Also make sure you understand what is going on with strtol()

char string[100]="+1236,,..,,actgn+3ACT-4CCCC";
char *token, *ptr;
int value;
       
token = strtok(string,"+-");
printf ("found numbers: ");
       
while (token != NULL)
{       
        value = strtol(token,&ptr,10);
        if (ptr != token)
                printf(" %d,",value);

        token = strtok(NULL,"+-");
}
printf("\n");


.

monkey_king Jul 2nd, 2009 7:58 pm
Re: extract multidigits from a char* (substring)
 
Thanks ppl,
I need to keep track of the indices,
so the strtok approch wont work.

But thanks again.

jephthah Jul 2nd, 2009 11:52 pm
Re: extract multidigits from a char* (substring)
 
well, that's a nice bit of feature creep to throw out there at the last minute.

do you work in marketing?

YrthWyndAndFyre Jul 3rd, 2009 5:08 pm
Re: extract multidigits from a char* (substring)
 
Quote:

Originally Posted by monkey_king (Post 906937)
Thanks ppl,
I need to keep track of the indices,
so the strtok approch wont work.

But thanks again.

The strtok() approach will work, just with a little added overhead - specifically, tracking the size of tokens you have already passed and either used or discarded.


All times are GMT -4. The time now is 12:54 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC