lcsvcn
lcsvcn

Reputation: 1224

How to split a string into int[3]

I have a string, like "101 1 13" and I need to split it to a int aux[3] --> resulting in aux[0] = 101, aux[1] = 1 and aux[2] = 13 (in this case). How can I do that?

In the example of the code below I get op as a String and want to get the value of the INTs in there. Each int is divided in the string by a white space(" ").

Another detail: I need the code to compile with flag -std=c99, so the answer that was accepted would not work.

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


//example of str = "101 1 14" (char *)
// example of output = {101, 1, 14}(int *)
 int* stoi(char *str) {
     // function to split str into 3 ints

}
    int main() {
        char op[10];
        int num[3];

        scanf("%s\n", op);

        num = stoi(op);
        printf("%d %d %d", num[0], num[1], num[2]); 

        return 0;
    }

Upvotes: 1

Views: 220

Answers (5)

lcsvcn
lcsvcn

Reputation: 1224

I found an easier approach to the problem. I insert a scanf, that don't catch the space blanket and convert it using atoi. As it is just 3 ints it doesn't become so bad to use this simple, repetitive way of catching the values. And it work with the -std=c99 flag, that I needed to use.

    scanf("%s[^ ]\n", op);
    num[0] = atoi(op);

    scanf("%s[^ ]\n", op);
    num[1] = atoi(op);

    scanf("%s[^ ]\n", op);
    num[2] = atoi(op);

    printf("%d\n", num[0]); 
    printf("%d\n", num[1]); 
    printf("%d\n", num[2]); 

Upvotes: 0

AnArrayOfFunctions
AnArrayOfFunctions

Reputation: 3754

This answer is assuming you know how much integers your string contain at the time of writing your code. It also uses specific clang/gcc extension (typeof) and may not be portable. But it may be helpful to someone (I mainly wrote it because I had nothing good to do).

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

struct {int _[3];} strToInt3(const char (*pStr)[])
{
    int result[3] = {0}, *pr = result;

    for(register const char *p = *pStr; *p; ++p)
    {
        if(*p == ' ') ++pr;
        else
            *pr *= 10,
            *pr += *p - '0';    
    }

    return *(__typeof__(strToInt3(0)) *)result;
}

int main()
{

    char op[10];
    int num[3];

    scanf("%10[^\n]", op),

    //memcpy(num, strToInt3(op)._, sizeof(num));
    //or
    *(__typeof__(strToInt3(0)) *)num = strToInt3(op);

    printf("%d %d %d", num[0], num[1], num[2]);  
}

I've commented the copying of returned array using memcpy and added a structure assignment. Although both must be valid (not standard I guess but working in most cases) I prefer the second option (and maybe some compiler optimizers will).

Also I assume ASCII character set for chars.

Upvotes: 0

Han Arantes
Han Arantes

Reputation: 775

See this example of how to do that:

char string [10] = "101 1 666"
int v [3], n=0, j=0;
int tam = strlen(string);
int current_Len = 0;

for(i=0; i<tam; i++){
    //32 = ascii for White space 
    if(string[i] != 32){
          n = n*10 + string[i] - '0';
          current_len++;
    } else if (current_len > 0){
          v[j++] = n;
          current_len = 0;
          n=0;
    }
}
if (current_len > 0){
          v[j++] = n;
}

Upvotes: 0

Cloud
Cloud

Reputation: 19333

First you need to tokenize your input (break apart the input into distinct elements). Then you need to parse/integerize the individual tokens by converting them from strings to the desired format.


Sample Code


#include <stdio.h>
#include <string.h>
#define BUF_LEN     (64)

int main(void)
{
    char buf[BUF_LEN] = { 0 };
    char* rest = buf;
    char* token;
    int i = 0;
    int iArr[100] = { 0 };

    if ( fgets(buf, BUF_LEN, stdin) != NULL )
    {
        strtok(buf, "\n"); // Remove newline from input buffer in case we want to call fgets() again.
        while ( (token = strtok_r(rest, " ", &rest)) != NULL )
        {
            iArr[i] = strtol(token, NULL, 10);
            printf("Token %d:[%d].\n", i, iArr[i]);
            i++;
        }
    }

    return 0;
}

Sample Run


1231 12312 312 1232 1312
Token 0:[1231].
Token 1:[12312].
Token 2:[312].
Token 3:[1232].
Token 4:[1312].

Upvotes: 2

Adiya Buyantogtokh
Adiya Buyantogtokh

Reputation: 171

Try to replace your code by following code.

The new code works only if input contains only single space between integers.

Your code:

while(op[cont] != '\0') {
                for(i = 0; op[cont] != ' '; i++, cont++) {

                    num[i] += op[cont];
                }
                printf("num[i] = %d\n", num[i]);
            }

New code:

while(op[cont] != '\0')
            {
                if(op[cont] != ' ')
                    num[i] = num[i]*10 + (op[cont]- '0');
                else 
                    i++;
                cont++;
            }

Upvotes: 1

Related Questions