extract multidigits from a char* (substring)

Reply

Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

extract multidigits from a char* (substring)

 
0
  #1
Jul 1st, 2009
Hi,
given a cstring, I need to extract the digits in it, the digits are prefixed with either a '+' or '-'. Like
  1. ,.,.,.,+3ACT,.,.,.,.-12,.,.,.,.,.,.,.,actgncgt
  2. #OUTPUT
  3. 3
  4. 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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main(){
  6. char tmp_array[100];
  7. const char* seq = "+1236,,..,,actgn+3ACT-4CCCC";
  8. printf("%s\n",seq);
  9.  
  10. for(int i=0;i<strlen(seq);i++){
  11. if(seq[i]!='+'&&seq[i]!='-')
  12. continue;
  13. int j=i+1;
  14. while(j<strlen(seq)){
  15. if(seq[j]>='0' &&seq[j]<='9'){
  16. j++;
  17. }else
  18. break;
  19.  
  20. }
  21. strncpy(tmp_array,seq+i+1,j-i-1);
  22. tmp_array[j-i-1]='\0';
  23. printf("numbers in substrings are: %d\n",atoi(tmp_array));
  24. }
  25.  
  26. return 0;
  27. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,588
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: extract multidigits from a char* (substring)

 
0
  #2
Jul 1st, 2009
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.




.
Last edited by jephthah; Jul 1st, 2009 at 7:33 pm. Reason: realized you dont want to save the + or -
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: extract multidigits from a char* (substring)

 
0
  #3
Jul 1st, 2009
untested but here...

  1. int main()
  2. {
  3. char tmp_array[100];
  4. const char* seq = "+1236,,..,,actgn+3ACT-4CCCC";
  5. printf("%s\n",seq);
  6.  
  7. while (*seq)
  8. { // pre-scan for +-
  9. if (*seq != '-' && *seq != '+')
  10. {
  11. seq++;
  12. continue;
  13. }
  14.  
  15. char *p = tmparray;
  16. *p++ = *seq++;
  17.  
  18. while (*seq)
  19. {
  20. if ((*seq < '0') || ('9' < *seq))
  21. {
  22. break;
  23. }
  24.  
  25. *p++ = *seq++;
  26. }
  27.  
  28. *p = 0;
  29.  
  30. printf("numbers in substrings are: %d\n",atoi(tmp_array));
  31. }
  32.  
  33. return 0;
  34. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: extract multidigits from a char* (substring)

 
0
  #4
Jul 1st, 2009
But if allowed to use off the shelf library functions, then poster jephthah has the right idea!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: extract multidigits from a char* (substring)

 
0
  #5
Jul 1st, 2009
why dont you put an array with stuff you want and extract that to new array?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: extract multidigits from a char* (substring)

 
0
  #6
Jul 1st, 2009
Double the processing time. No optimal!

Why process a list twice when you can do it once!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,588
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: extract multidigits from a char* (substring)

 
0
  #7
Jul 1st, 2009
wildgoose: posting "Untested" code on daniweb's C forum is generally risky business

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

  1. char string[100]="+1236,,..,,actgn+3ACT-4CCCC";
  2. char *token, *ptr;
  3. int value;
  4.  
  5. token = strtok(string,"+-");
  6. printf ("found numbers: ");
  7.  
  8. while (token != NULL)
  9. {
  10. value = strtol(token,&ptr,10);
  11. if (ptr != token)
  12. printf(" %d,",value);
  13.  
  14. token = strtok(NULL,"+-");
  15. }
  16. printf("\n");


.
Last edited by jephthah; Jul 1st, 2009 at 8:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: extract multidigits from a char* (substring)

 
0
  #8
Jul 2nd, 2009
Thanks ppl,
I need to keep track of the indices,
so the strtok approch wont work.

But thanks again.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,588
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: extract multidigits from a char* (substring)

 
0
  #9
Jul 2nd, 2009
well, that's a nice bit of feature creep to throw out there at the last minute.

do you work in marketing?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: YrthWyndAndFyre is an unknown quantity at this point 
Solved Threads: 0
YrthWyndAndFyre YrthWyndAndFyre is offline Offline
Newbie Poster

Re: extract multidigits from a char* (substring)

 
1
  #10
Jul 3rd, 2009
Originally Posted by monkey_king View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC