-2

I'm looking at writing a function that parses a given string input and separates the string into its constituents (as defined by one or more space characters).

Unfortunately, I have had no real experience with string parsing, so any help would be really appreciated!

Many thanks!

  • 2
    You didn't ask an actual question. Also SO is not a coding service, so you need to try something and show where you are having trouble with it. If you are completely unclear on what to do then using a search engine is the first step (or consulting your notes and/or teacher if this is for a course/class). – UnholySheep Mar 25 '17 at 11:45
  • Thanks for the feedback. I have a starting point now in the form of the functions mentioned in the other comments. In the next little while I'll go back and have a solid crack at it and let you know how I go :) –  Mar 25 '17 at 11:59

3 Answers3

1

Take a look to strsep(3) too, since it is "intended as a replacement for the strtok() function."

https://www.freebsd.org/cgi/man.cgi?strsep(3)

spallas
  • 188
  • 1
  • 4
1

The function can be declared and defined the following way as it is shown in the demonstrative program.

#include <stdio.h>
#include <ctype.h>

size_t string_parser( const char *input, char *word_array[] ) 
{
    size_t n = 0;

    while ( *input )
    {
        while ( isspace( ( unsigned char )*input ) ) ++input;
        if ( *input )
        {
            word_array[n++] = ( char * )input;
            while ( *input && !isspace( ( unsigned char )*input ) ) ++input;
        }           
    }

    return n;
}  

#define N   10

int main(void) 
{
    char s[] = "Hello Sam Talbot. How do you do?";
    char * word_array[N];

    size_t n = string_parser( s, word_array );

    for ( size_t i = 0; i < n; i++ ) puts( word_array[i] );

    return 0;
}

Its output is

Hello Sam Talbot. How do you do?
Sam Talbot. How do you do?
Talbot. How do you do?
How do you do?
do you do?
you do?
do?

Another approach to defining the function is to allocate dynamically the array of words inside the function. In this case the function declaration can look like

size_t string_parser( const char *input, char ***word_array ) 

You should at first count the words using one loop and then in another loop fill the array of words.

For example

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

size_t string_parser( const char *input, char ***word_array) 
{
    size_t n = 0;
    const char *p = input;

    while ( *p )
    {
        while ( isspace( ( unsigned char )*p ) ) ++p;
        n += *p != '\0';
        while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
    }

    if ( n )
    {
        size_t i = 0;

        *word_array = malloc( n * sizeof( char * ) ); 

        p = input;

        while ( *p )
        {
            while ( isspace( ( unsigned char )*p ) ) ++p;
            if ( *p )
            {
                const char *q = p;
                while ( *p && !isspace( ( unsigned char )*p ) ) ++p;

                size_t length = p - q;

                ( *word_array )[i] = ( char * )malloc( length + 1 );

                strncpy( ( *word_array )[i], q, length );
                ( *word_array )[i][length] = '\0';

                ++i;
            }
        }           
    }

    return n;
}  

int main(void) 
{
    char s[] = "Hello Sam Talbot. How do you do?";
    char ** word_array = NULL;

    size_t n = string_parser( s, &word_array );

    for ( size_t i = 0; i < n; i++ ) puts( word_array[i] );

    for ( size_t i = 0; i < n; i++ ) free( word_array[i] );
    free( word_array );

    return 0;
}

The program output is

Hello
Sam
Talbot.
How
do
you
do?
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Hi there! Thank you for your demonstrative Program - it is very helpful. A question about the second example, it appears that you use a triple pointer to word_array in the function. I am struggling to see how this works. Would you be able to help me understand it's use in this example? Thanks! –  Mar 25 '17 at 19:25
  • @SamTalbot If you want that the argument would be changed in a function you have to pass it indirectly by reference/ Otherwise all arguments are passed by value and the function will deal with a copy of its argument. – Vlad from Moscow Mar 26 '17 at 02:06
  • Ah, okay. This makes more sense. Thanks! –  Mar 26 '17 at 02:10
0

Take a look at strtok, this does what you require.

You need to at least attempt this on your own.

Cdpas
  • 1
  • 2