| | |
extract multidigits from a char* (substring)
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
Hi,
given a cstring, I need to extract the digits in it, the digits are prefixed with either a '+' or '-'. Like
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
given a cstring, I need to extract the digits in it, the digits are prefixed with either a '+' or '-'. Like
C Syntax (Toggle Plain Text)
,.,.,.,+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
c Syntax (Toggle Plain Text)
#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; }
•
•
•
•
Does anyone have an idea if this can be done smarter, better, faster?
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 -
untested but here...
C Syntax (Toggle Plain Text)
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: 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()
.

anyhow, being able to use strtok() correctly is tricky for a new user. Also make sure you understand what is going on with strtol()
c Syntax (Toggle Plain Text)
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");
.
Last edited by jephthah; Jul 1st, 2009 at 8:02 pm.
![]() |
Similar Threads
- extract numbers from char (C++)
- How to extract words from char type string (C++)
- Converting specified elements of array of char (C++)
Other Threads in the C Forum
- Previous Thread: adding from 0 to a number that will be entered by the user .
- Next Thread: Program Keeps reading wrong input
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






