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;
}