c - 如何计算给定字符串中的单词总数和每个单词的字符数并创建一维数组?

标签 c

如何将字数和字符数返回到一个数组中?

#define NUMBER_MOVIES 3
void getWordCount( char movies[NUMBER_MOVIES][40]);


int main ()
{

    char movies[NUMBER_MOVIES][40] = {"Jurasic World", "Captain America", "I spite on your grave"};

    getWordCount(movies);


    return 0;
}

void getWordCount(char movies[NUMBER_MOVIES][40])
{
    int i;
    int wordCount = 0;

    int wordCharCount[ ]; // need to store to this array

    for(i = 0; i < strlen(movies[i]); i++)
    {
        if (*movies[i] == ' ')
        {
            wordCount++;
            printf ("word count :%s, %d \n", movies[i], wordCount);
        }

        printf ("word count :%s, %d \n", movies[i], wordCount);
    }

}

我想将所有字数和字符数存储到同一个数组中,即 wordCharCount []

例如:侏罗纪世界;数组应该是 [2 8 5] 。所以 2 个单词,第一个单词包含 8 个字符,第二个单词包含 5 个字符。此外,数组应该可以从函数外部访问。

最佳答案

最明显的问题是你的字数统计只有一个循环,但你需要扫描每部电影中的每个角色 - 这表明有两个嵌套循环。

那么在单个数组中返回两种类型的信息的想法是不明智的 - 结构会更合适。然后,在 C 中,您无法按值传递或返回数组,因此通常让调用者按引用传递数组,以便函数放置结果。

考虑:

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

struct sMovie
{
    char title[40] ;
    int title_stats[40] ;
} ;

void getWordCount( struct sMovie* movies, int number_of_movies );

int main ()
{
    struct sMovie movies[] = { {"Jurassic World"}, 
                               {"Captain America"}, 
                               {"I spit on your grave"} } ;

    const int number_of_movies = sizeof(movies) / sizeof(*movies) ;
    getWordCount( movies, number_of_movies ) ;

    for( int m = 0; m < number_of_movies; m++ )
    {
        printf( "\"%s\" has %d words of lengths",
        movies[m].title,
        movies[m].title_stats[0] ) ;
        for( int w = 1; w <= movies[m].title_stats[0]; w++ )
        {
            printf( " %d", movies[m].title_stats[w] ) ;
        }
        printf( "\n" ) ;
    }

    return 0;
}

void getWordCount( struct sMovie* movies, int number_of_movies )
{
    // For each movie...
    for( int m = 0; m < number_of_movies; m++)
    {
        memset( movies[m].title_stats, 0, sizeof(movies[m].title_stats) ) ;
        movies[m].title_stats[0] = 1 ;

        // For each character
        for( int i = 0; movies[m].title[i] != 0; i++ )
        {
            int word = movies[m].title_stats[0] ; 
            movies[m].title_stats[word]++ ;

            if( movies[m].title[i] == ' ' )
            {
                movies[m].title_stats[word]-- ;
                movies[m].title_stats[0]++ ;
            }
        }
    }
}

输出:

"Jurassic World" has 2 words of lengths 8 5                                                                                                              
"Captain America" has 2 words of lengths 7 7                                                                                                             
"I spit on your grave" has 5 words of lengths 1 4 2 4 5                                                                                                  

关于c - 如何计算给定字符串中的单词总数和每个单词的字符数并创建一维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55388258/

相关文章:

c - 链表: Inserting a name and id number to list

c - 当函数返回一个结构时,它保存在堆栈或堆中?

c - 并行递归算法

c - C 中 do-while 循环内的 fgets() 问题

c - fscanf() 不适用于输入

c - UDP 客户端不会自动重新连接

c - 在 C 中,为什么我的clearList函数不起作用

c - 了解C指针

python - c mmap 将 long 写入 mmaped-area

c - 对话框中的 gtk_entry