将C中的字符串解析为单个单词

时间:2017-03-25 11:34:47

标签: c arrays string parsing string-parsing

我正在研究编写一个解析给定字符串输入的函数,并将字符串分离为其成分(由一个或多个空格字符定义)。

细节:

  • 为NULL终止字符串
  • 指定函数“* input”
  • 函数需要解析给定的字符串,返回单个字的数量
  • 函数还需要更新一个指针数组,“* word_array []”,它将指向输入字符串中每个单词的开头
  • 我需要为所述单词数组以及字符串
  • 分配内存

我需要使用的函数原型应该是这样的:

int string_parser(char *input, char *word_array[]);

如果我要猜测函数的布局,我认为它看起来像这样:

int string_parser(char *input, char *word_array[])
{
    // pseudocode:

    // Allocate memory for word array, and strings

    // Check input for a space(s)

    // Separate into individual words and pass into word_array 

    // Return the number of elements within the array as this will indicate the number of words

}

不幸的是我没有使用字符串解析的真实经验,所以任何帮助都会非常感激!

非常感谢!

3 个答案:

答案 0 :(得分:1)

看看strsep(3)也是如此,因为它是"用来代替strtok()函数。"

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

答案 1 :(得分:1)

可以按照以下方式声明和定义函数,如演示程序中所示。

#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;
}

它的输出是

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?

定义函数的另一种方法是动态分配函数内的单词数组。在这种情况下,函数声明可以看起来像

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

您应该首先使用一个循环计算单词,然后在另一个循环中填充单词数组。

例如

#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;
}

程序输出

Hello
Sam
Talbot.
How
do
you
do?

答案 2 :(得分:0)

看看strtok,这可以满足您的需求。

您至少需要自己尝试。