首页 > 解决方案 > C字计数器和标点计数器

问题描述

我能够使用这种结构进行编程,并且承认我必须使用ispunct()andisspace()来计算标点符号和单词的数量。但我不确定如何使用这些功能进行编程。

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

int WordCount(char *input, int size)
{
    //programming here//
}

int PunctuationCount(char *input, int size)
{
    //programming here//
}

int main(void)
{
    char test1[] = "Hello, World!";
    char test2[] = "She sell sea shell on a sea shore. The shells she sells are sea-shells. I'm sure? For if she sells sea-shells on the sea-shore, then I'm sure she sells sea-shore shells!";
    char test3[] = "VISION 2020 embodies every SKKU student's dream, will, and destiny to make SKKU a global leading university. To successfully establish VISION 2020, SKKU will pursue 'The 5 Core Strategies' and '5 Divisional Strategies'. The Strategic Tasks for 5 Major Areas are the general tasks that influence SKKU's competitiveness.";

    printf("test1 has %d punctuations, %d words.\n", PunctuationCount(test1,sizeof(test1)/sizeof(char)), WordCount(test1,sizeof(test2)/sizeof(char)));
    printf("test2 has %d punctuations, %d words.\n", PunctuationCount(test2,sizeof(test2)/sizeof(char)), WordCount(test2,sizeof(test2)/sizeof(char)));
    printf("test3 has %d punctuations, %d words.\n", PunctuationCount(test3,sizeof(test3)/sizeof(char)), WordCount(test3,sizeof(test2)/sizeof(char)));

    return 0;
}

标签: c

解决方案


请注意,此算法不处理连字符或缩略词。(那是你的工作!)

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

int WordCount(char *input, int size)
{
    int inword=0;
    int count=0;
    for (;*input; input++)
        if (isalpha((int) *input))
        {   // does not handle hyphenated words or contractions
            inword=1;
        }
        else
        {
            if (inword)
            {
                inword=0;
                count++;
            }
        }

    return count;
}

int PunctuationCount(char *input, int size)
{
    int count=0;
    for (;*input; input++)
        if (ispunct((int) *input))
        {   // does not handle hyphenated words or contractions
            count++;
        }

    return count;
}

int main(void)
{
    char test1[] = "Hello, World!";
    char test2[] = "She sell sea shell on a sea shore. The shells she sells are sea-shells. I'm sure? For if she sells sea-shells on the sea-shore, then I'm sure she sells sea-shore shells!";
    char test3[] = "VISION 2020 embodies every SKKU student's dream, will, and destiny to make SKKU a global leading university. To successfully establish VISION 2020, SKKU will pursue 'The 5 Core Strategies' and '5 Divisional Strategies'. The Strategic Tasks for 5 Major Areas are the general tasks that influence SKKU's competitiveness.";

    printf("test1 has %d punctuations, %d words.\n", PunctuationCount(test1,sizeof(test1)/sizeof(char)), WordCount(test1,sizeof(test2)/sizeof(char)));
    printf("test2 has %d punctuations, %d words.\n", PunctuationCount(test2,sizeof(test2)/sizeof(char)), WordCount(test2,sizeof(test2)/sizeof(char)));
    printf("test3 has %d punctuations, %d words.\n", PunctuationCount(test3,sizeof(test3)/sizeof(char)), WordCount(test3,sizeof(test2)/sizeof(char)));

    return 0;
}

推荐阅读