Split a string with empty spaces

Asked

Viewed 5,371 times

4

I’m trying to divide an entrance of the following kind:

1444771699,Andre Alves,SAQUE,-500.00,,200

I’m trying to use the strtok, but it ignores the empty space. Any idea of how to split the string without ignoring the space?

  • 2

    Welcome to the Sopt. Put more information in the question so it is better for the community to help you, for example, show the string you receive as input and show the output you want. I also suggest you take a tour to learn how to ask: http://answall.com/tour

  • 2

    What do you mean by "ignoring" space? Does that mean that the "pieces" come with space, but they should come without, or would it be the other way around? Or do you refer to the section that has nothing (i.e. between the two commas)? Please say what exactly you want, the strtok looks like it can solve if called with the right arguments. I suggest [Edit] the question showing how you did, what you got, and what you hoped to get.

5 answers

0

The Strtok method, serves to break a string, after passing its string once per parameter, just put NULL ou 0 in the first parameter, the method will return the parts of the string. Use string.h

char *buff = "minha string com espaço";
char *word = strtok(buff," ");
int x=0;
while(word){
    puts(word);
    word = strtok(NULL, " ");
    x++;
}

0

The function strtok ends up separating a string into "pieces", or tokens, according to some expression used.

Function prototype: char* strtok(char* str, const char* delimiters);

So a simple solution to your problem would be something like:

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

int main(int argc, char* argv[]) {
    char string[] = "1444771699,Andre Alves,SAQUE,-500.00,,200";
    printf("String: %s\n", string);
    char* token;
    token = strtok(string, ",");
    while (token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, ",");
    }
    return 0;
}

Considering your entry as 1444771699,Andre Alves,SAQUE,-500.00,,200, the execution of the above code will result in the output below
1444771699 Andre Alves SAQUE -500.00 200

You can then allocate the output below in a string array if necessary according to your application.

0

Knowing which fields may not receive values between commas could make a specific reading for this field, see an example using sscanf to read data from a string containing the input you provided:

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

int main(int argc, char* argv[]) {
    char s[] = "1444771699,Andre Alves,SAQUE,-500.00,,200";
    char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
    double d;
    int n, b;

    sscanf(s, " %[^,],%[^,],%[^,],%lf,%n", buffer1, buffer2, buffer3, &d, &n);
    if(s[n] == ',') {
        buffer4[0] = '\0';
        sscanf(s+n+1, "%d", &b);
    }
    else {
        sscanf(s+n, "%[^,],%d", buffer4, &b);
    }
    printf("%s \n%s \n%s \n%lf \n%s \n%d \n", buffer1, buffer2, buffer3, d, buffer4, b);


    return 0;
}

Note that as we already know that the fifth comma-separated field can be empty it is not read in the first sscanf, and we first check if it is empty before reading, and if empty assigns empty string in the buffer4 and skip the next comma (s+n+1) to read the 6th course.

0

You can format the string character by character.

Example:

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

int main() {
    char s[] = "1444771699,Andre Alves,SAQUE,-500.00,,200";
    int qtd = 1;

    printf("string %d: ", qtd);
    for (int i = 0; i <  strlen(s); i++){
        if ( s[i] != ',')
            printf("%c", s[i]);
        else if ( s[i] == ',') {
            qtd++;
            printf("\nstring %d: ", qtd);
        }
    }
    return 0;
}

-1

I suppose the problem is the empty fields, that the strtok() does not recognize. On Linux, we have the function strsep(), which was created precisely to resolve the limitation of strtok():

#include <string.h>
char *strsep(char **stringp, const char *delim);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.