Vivek Shankar
Vivek Shankar

Reputation: 800

How to extract multi-digit numbers from a string?

Firstly, I know similar questions have been asked before but I believe my case is different.

My input string is:

(5,7) (1,6) (2,4) (10,14) (8,9)

I wrote the following code for extraction into an array.

main(){
char s[100];
int i=0,x,n=0;
int a[20];
printf("Enter the sets:");
gets(s);
x=strlen(s);
while(i<x){
    if((s[i]=='(' && s[i+2]==',') || (s[i]==',' && s[i+2]==')'))
    {
        a[n]=s[i+1]-'0';
        n++;
    }   
    i++;
}
for(i=0;i<n;i++){
    printf("%d\n",a[i]);
}
}

The output I get is:

5 7 1 6 2 4 8 9

I understand why my code will skip numbers having 2 or more digits. Please suggest some minor changes to the present code to fix this limitation.

P.S.- I'm looking for a solution which doesn't depend on length of the number.

Upvotes: 2

Views: 1708

Answers (5)

dbush
dbush

Reputation: 223872

Since you only care about the numbers and not any of the delimiters, you can use strtok, which allows for a set of delimiters.

Use the following in place of you existing while loop:

char *p = strtok(s, "(), ");
while (p) {
    a[n++] = atoi(p);
    p = strtok(NULL, "(), ");
}

Output:

5
7
1
6
2
4
10
14
8
9

If on the other hand you are particular about the format, you can do the following:

char *start = s, *p1 = NULL, *p2 = NULL, *p3 = NULL;
if (start) p1 = strchr(start, '(');
if (p1) p2 = strchr(p1+1, ',');
if (p2) p3 = strchr(p2+1, ')');
while (p1 && p2 && p3) {
    a[n++] = atoi(p1+1);
    a[n++] = atoi(p2+1);
    start = p3+1;
    if (start) p1 = strchr(start, '(');
    if (p1) p2 = strchr(p1+1, ',');
    if (p2) p3 = strchr(p2+1, ')');
}

Upvotes: 4

Tim
Tim

Reputation: 1933

If you always have the same format (a,b)(c,d)...(y,z) and the same number of values then this solution works :

char * arr = "(5,7)(1,6)(2,4)(10,14)(8,9)";
int a,b,c,d,e,f,g,h,i,j;

sscanf(arr,"(%d,%d)(%d,%d)(%d,%d)(%d,%d)(%d,%d)",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j);

printf("%d %d %d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h, i, j);

Upvotes: 0

rht
rht

Reputation: 106

If the input is in the exact format as in the question, then you can add two loops inside the main while loop to read one set at a time.

while (i < x)
{
    if (s[i] == '(')
    {
        // temporary var to store number
        int num = 0;

        // read first number
        while (s[++i] != ',')
            num = num*10 + s[i]-'0';
        a[n++] = num;

        num = 0;
        // read second number
        while (s[++i] != ')')
            num = num*10 + s[i]-'0';
        a[n++] = num;
    }
    i++;
}

Upvotes: 0

Lokesh Sanapalli
Lokesh Sanapalli

Reputation: 1034

#include <stdio.h>

int main(void) {
    // your code goes here
char s[100];
int i=0,x,n=0;
int a[20];
printf("Enter the sets:");
gets(s);
x=strlen(s);
while(i<x-1){
    if(isdigit(s[i]))
    {
        if(isdigit(s[i+1]))
        {
            a[n]=(s[i]-'0')*10  +(s[i+1]-'0');
            i++;
        }
        else
        {
            a[n]=s[i]-'0';
        }
        n++;
    }   
    i++;
}
printf("\n");
for(i=0;i<n;i++){
    printf("%d\n",a[i]);
}
    return 0;
}

What about the above code, unfortunately C doesn't have simple string functions like split with Regex(it has split function but i didn't understand well). Alternatively, here is ideone for it https://ideone.com/eRKTbD

Upvotes: 0

TeaNyan
TeaNyan

Reputation: 5079

I have used a different approach to the problem, but I have solved it and it works. Consider trying this. Btw I have used char *s as a string literal but you can keep it like yours.

main(){

  char *s="(5,7) (1,6) (2,4) (10,14) (8,9)";
  int i=0,x,n=0;
  char a[20];
  x=strlen(s);
  while(i<x){

    if (isdigit(s[i])) {
      a[n]=s[i];
      if (s[i+1]==',' || s[i+1]==')') {
        a[n+1]=' ';
        n++;
      }
      n++;
    }
      i++;
  }
  printf("%s\n", a);
}

output:

tenshi@mashiro:~/projects/test$ ./test 
5 7 1 6 2 4 10 14 8 9 

Upvotes: 0

Related Questions