943,515 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 703
  • C RSS
Jul 1st, 2009
0

extract multidigits from a char* (substring)

Expand Post »
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. }
Similar Threads
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 1st, 2009
0

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.




.
Last edited by jephthah; Jul 1st, 2009 at 7:33 pm. Reason: realized you dont want to save the + or -
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 1st, 2009
0

Re: extract multidigits from a char* (substring)

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. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 1st, 2009
0

Re: extract multidigits from a char* (substring)

But if allowed to use off the shelf library functions, then poster jephthah has the right idea!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 1st, 2009
0

Re: extract multidigits from a char* (substring)

why dont you put an array with stuff you want and extract that to new array?
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Jul 1st, 2009
0

Re: extract multidigits from a char* (substring)

Double the processing time. No optimal!

Why process a list twice when you can do it once!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 1st, 2009
0

Re: extract multidigits from a char* (substring)

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 2nd, 2009
0

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.
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 2nd, 2009
0

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?
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 3rd, 2009
1

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.
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.
Reputation Points: 24
Solved Threads: 1
Newbie Poster
YrthWyndAndFyre is offline Offline
7 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: adding from 0 to a number that will be entered by the user .
Next Thread in C Forum Timeline: Program Keeps reading wrong input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC