Jainendra
Jainendra

Reputation: 25153

How to read numbers separated by space using scanf

I want to read numbers(integer type) separated by spaces using scanf() function. I have read the following:

It doesn't help me much.

How can I read numbers with space as delimiter. For e.g. I have following numbers as input 2 5 7 4 3 8 18 now I want to store these in different variables. Please help.

Upvotes: 34

Views: 152991

Answers (5)

Mateus Alves de Oliveira
Mateus Alves de Oliveira

Reputation: 1031

With this solution, it's possible to read positive and negatives integers:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 50


int convert_input (int * v, char * buffer) {
    int len = 0, i = 0, temp = 0, positive_or_negative_one = 1;
    while(buffer[i]!='\0') {
        temp = 0;

        if (buffer[i] == '-'){
            positive_or_negative_one = -1;
            i++;
        } else {
            while(buffer[i] != ' ' && buffer[i] != '\0')
                temp = temp*10 + (buffer[i++]-'0');

            if(buffer[i]==' ')
                i++;
            v[len++] = temp * positive_or_negative_one;
            positive_or_negative_one = 1;
        }
    }
    return len;
}

int main(int argc, char const *argv[]) {
    int *a = NULL;
    int count_a, len=0;
    char buffer[BUFFER_SIZE];

    printf("Input numbers here: ");
    gets(buffer);
    for (int i = 0; i < strlen(buffer); i++) {
        if (buffer[i] == ' '){
            len+=1;
        }
    }

    a = (int*) malloc(sizeof(int) * len + 1);
    count_a = convert_input(a, buffer);

    for (int i = 0; i < count_a; i++) {
        printf("%d\n", a[i]);
    }

    free(a);

    return 0;
}

Input and output example:

Input numbers here: 1 2 3 -4 10
1
2
3
-4
10

Upvotes: 0

user694833
user694833

Reputation:

It should be as simple as using a list of receiving variables:

scanf("%i %i %i", &var1, &var2, &var3);

Upvotes: 2

Chris Dodd
Chris Dodd

Reputation: 126525

scanf uses any whitespace as a delimiter, so if you just say scanf("%d", &var) it will skip any whitespace and then read an integer (digits up to the next non-digit) and nothing more.

Note that whitespace is any whitespace -- spaces, tabs, newlines, or carriage returns. Any of those are whitespace and any one or more of them will serve to delimit successive integers.

Upvotes: 7

niraj.nijju
niraj.nijju

Reputation: 667

int main()
{
char string[200];
int g,a,i,G[20],A[20],met;

gets(string);
g=convert_input(G,string);

for(i=0;i<=g;i++)
    printf("\n%d>>%d",i,G[i]);
return 0;
}

int convert_input(int K[],char string[200])
{
int j=0,i=0,temp=0;
while(string[i]!='\0')
{
    temp=0;
    while(string[i]!=' ' && string[i]!='\0')
        temp=temp*10 + (string[i++]-'0') ;
    if(string[i]==' ')
        i++;
    K[j++]=temp;
}
return j-1;
}

Upvotes: 3

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

I think by default values read by scanf with space/enter. Well you can provide space between '%d' if you are printing integers. Also same for other cases.

scanf("%d %d %d", &var1, &var2, &var3);

Similarly if you want to read comma separated values use :

scanf("%d,%d,%d", &var1, &var2, &var3);

Upvotes: 40

Related Questions