Split whitespaces in a string and store them in table in C without libraries

split string in c and store in array without strtok
c program to split a string into words
c split string by space into array
c split string by whitespace
c program to split a string into characters
how to extract words from a string in c
c split string by comma
how to extract words from a string in c++

I had a assignment for my class last week where I had to split a string with the spaces, tabs et \n as separators and store every "word" in an array. I think I'm very close but my output is very weird so if someone could tell what did I forget, it would be nice. Only thing is I can only use malloc.

char    **ft_split_whitespaces(char *str)
{
    int     i;
    int     j;
    int     k;
    char    **tab;

    i = 0;
    j = 0;
    k = 0;
    tab = (char**)malloc(sizeof(*tab) * (ft_nb_words(str) + 1));
    while (str[i])
    {
        while (str[i] == ' ' || str[i] == '\t' || str[i] == '\n')
            i++;
        if (str[i])
        {
            if ((tab[j] = (char*)malloc(sizeof(char) * (ft_len_word(str + i) + 1))) == NULL)
                return (NULL);
            while (k < ft_len_word(str + i))
                tab[j][k++] = str[i++];
            tab[j++][k] = '\0';
            k = 0;
        }
    }
    tab[j] = NULL;
    return (tab);
}

The functions returning the length of a word and the number of words work fine so I think the problem comes from the main function.


This can be easily handled if you take one pointer to point the last occurrence of specific character('' '\n' \t).

   char    **ft_split_whitespaces(char *str)
    {
    int     i;
    int     j;
    int     k;
    char    **tab;
    char *prevToken=str;

    i = 0;
    j = 0;
    k = 0;
    tab = (char**)malloc(sizeof(*tab) * (ft_nb_words(str) + 1));
    while (str[i] != '\0')
    {

        if(str[i] == ' ' || str[i] == '\t' || str[i] == '\n')
        {
            i++;
            if ((tab[j] = (char*)malloc(sizeof(char) * (ft_len_word(prevToken) + 1))) == NULL)
                return (NULL);

            while (k < ft_len_word(prevToken) &&
                (prevToken[k] !=' ' &&  prevToken[k] != '\t' &&  prevToken[k] != '\n'))
                tab[j][k] = prevToken[k++];

            printf("tab=%s\n", tab[j]);
            k = 0;
            j++;
            prevToken=(str+i);
        }
        else{
           i++;
         }
    }

      /* to handle the last word */
            if ((tab[j] = (char*)malloc(sizeof(char) * (ft_len_word(prevToken) + 1))) == NULL)
                return (NULL);

            while (k < ft_len_word(prevToken) &&
                (prevToken[k] !=' ' &&  prevToken[k] != '\t' &&  prevToken[k] != '\n'))
                tab[j][k] = prevToken[k++];
            printf("tab=%s\n", tab[j]);


    tab[j] = NULL;
    return (tab);
    }

C program to split string into the words separated by spaces , How to extract words from a string, here is the c program which will split string will be stored into two dimensional character array (string array) the values will be “This”, “is”, “Mike”. First if character is not space then character is storing into the two dimensional character array and second, if it is space or  How to split a string in C++? I have an input file of data and each line is an entry. in each line each "field" is seperated by a white space " " so I need to split the line by space. other languages have a function called split (C#, PHP etc) but I cant find one for C++. How can I achieve this? Here is my code that gets the lines:


C exercises: Split string by space into words, C programming, exercises, solution: Write a program in C to split string by space into words. getline () reads everything on a single line, returning that whether it's empty or contains dozens of space-separated elements. If you provide the optional alternative delimiter ala getline (std::cin, my_string, ' ') it still won't do what you seem to want, e.g. tabs will be read into my_string. Probably not needed for this, but a fairly common


Your code is inefficient as you call ft_len_word far too many times, but it does not seem broken apart from the undefined behavior on malloc failures.

The problem might lie in your versions of ft_len_word or ft_nb_words. You should post a full program exhibiting the problem for a proper investigation.

Here is a modified version that does not use these functions:

#include <stdlib.h>

int ft_is_space(char c) {
    return (c == ' ' || c == '\t' || c == '\n');
}

char **ft_split_whitespaces(const char *str) {
    int i, j, k, len, in_space, nb_words;
    char **tab;

    nb_words = 0;
    in_space = 1;
    for (i = 0; str[i]; i++) {
        if (ft_is_space(str[i]) {
            in_space = 1;
        } else {
            nb_words += in_space;
            in_space = 0;
        }
    }
    tab = malloc(sizeof(*tab) * (nb_words + 1));
    if (tab != NULL) {
        i = 0;
        j = 0;
        while (str[i]) {
            while (ft_is_space(str[i]))
                i++;
            if (str[i]) {
                for (len = 1; str[i + len] && !ft_is_space(str[i + len]); len++)
                     continue;
                if ((tab[j] = malloc(sizeof(*tab[j]) * (len + 1))) == NULL) {
                     while (j > 0)
                         free(tab[--j]);
                     free(tab);
                     return NULL;
                }
                for (k = 0; k < len; k++)
                    tab[j][k] = str[i + k];
                tab[j++][len] = '\0';
                i += len;
            }
        }
        tab[j] = NULL;
    }
    return tab;
}

C Plus Plus Primer, Table 3.1 on the facing page lists the most commonly used string constructors. C, character string literals are not the same type as the standard library string type. read whitespace-separated string into s reads the standard input storing what newlines, tabs) • It then reads characters until the next whitespace character  C program to remove spaces or excess blanks from a string, For example, consider the string. It will remove spaces when they occur more than one time consecutively in string anywhere. If you want you can copy blank into text string so that original string is modified. Download Remove spaces program.


You need to implement your version of strtok() if you do not want to use the library function or need a different functionality than the one provided by strtok().

Below is a simple string tokenizer, which, unlike the standard library's strtok(), still returns a value in case of consecutive delimiters. I used this function to parse CSV files, which sometimes include empty cells, hence consecutive , characters. Standard library's strtok() did not work for me, so I had to implement my own function.

I used other helper functions, which are now part of a simple string library I maintain on GitHub, called zString.

Below is how it behaves

Example Usage
      char str[] = "A,B,,,C";
      printf("1 %s\n",zstring_strtok(s,","));
      printf("2 %s\n",zstring_strtok(NULL,","));
      printf("3 %s\n",zstring_strtok(NULL,","));
      printf("4 %s\n",zstring_strtok(NULL,","));
      printf("5 %s\n",zstring_strtok(NULL,","));
      printf("6 %s\n",zstring_strtok(NULL,","));

  Example Output
      1 A
      2 B
      3 ,
      4 ,
      5 C
      6 (null)

and the code

char *zstring_strtok(char *str, const char *delim) {
    static char *static_str=0;      /* var to store last address */
    int index=0, strlength=0;       /* integers for indexes */
    int found = 0;                  /* check if delim is found */

    /* delimiter cannot be NULL
    * if no more char left, return NULL as well
    */
    if (delim==0 || (str == 0 && static_str == 0))
        return 0;

    if (str == 0)
        str = static_str;

    /* get length of string */
    while(str[strlength])
        strlength++;

    /* find the first occurrence of delim */
    for (index=0;index<strlength;index++)
        if (str[index]==delim[0]) {
            found=1;
            break;
        }

    /* if delim is not contained in str, return str */
    if (!found) {
        static_str = 0;
        return str;
    }

    /* check for consecutive delimiters
    *if first char is delim, return delim
    */
    if (str[0]==delim[0]) {
        static_str = (str + 1);
        return (char *)delim;
    }

    /* terminate the string
    * this assignment requires char[], so str has to
    * be char[] rather than *char
    */
    str[index] = '\0';

    /* save the rest of the string */
    if ((str + index + 1)!=0)
        static_str = (str + index + 1);
    else
        static_str = 0;

        return str;
}

C++ Libraries, String and Standard Template Library, int isspace (int ch); Return 1 if ch is a white space (blank ' ', carriage return '\r', newline '\n' Unlike regular arrays, there is no need to pass the length of C-​string into const char * accept) Similar to strspn, but string pointer break returns a pointer to It extracts characters from the input stream and store into str , until either a  The idea is to traverse the string from left to right and ignore spaces while traversing. We need to keep track of two indexes, one for current character being red and other for current index in output. // C++ program to evaluate a given expression. #include <iostream> using namespace std; char *removeSpaces (char *str) int i = 0, j = 0;


7. Strings, The first letter of "banana" is not a, unless you are a computer scientist. It causes the runtime error IndexError: string index out of range. The constant string.whitespace contains all the whitespace characters, including To see a complete list, see the String Formatting Operations section of the Python Library Reference. Splitting a string by some delimiter is a very common task. For example, we have a comma separated list of items from a file and we want individual items in an array. Almost all programming languages, provide function split a string by some delimiter. In C/C++: // Splits str[] according to given delimiters.


C library function - sscanf(), C library function - sscanf() - The C library function int sscanf(const char *str, const char *format, . format − This is the C string that contains one or more of the following items: Whitespace character, that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument. See next table. What regex pattern would need I to pass to java.lang.String.split () to split a String into an Array of substrings using all whitespace characters ( ' ', '\t', ' ', etc.) as delimiters? Something in the lines of. This groups all white spaces as a delimiter. So if I have the string: This should yield the strings "Hello" and "World" and omit the


Python - Strings, Following table is a list of escape or non-printable characters that can be represented with backslash notation. An escape character gets interpreted; in a single  newStr = split(str) divides str at whitespace characters and returns the result as the output array newStr. The input array str can be a string array, a character vector, or a cell array of character vectors. If str is a string array, then so is newStr. Otherwise, newStr is a cell array of character vectors.