c - 如何计算字符串中特殊字符的数量?

标签 c string special-characters

我正在编写一个程序,用于计算行数、单词数、字符数、数字、字母和特殊字符的数量。到目前为止,程序已基本完成,但特殊字符给我带来了麻烦。我使用带有 if 语句的 while 循环来计算这些字符,并在 else 语句中使用特殊字符。有人可以指出我正确的方向吗?

这是我正在使用的字符串:

欢迎来到 CIS158。 C和Tux很努力吧? 希望您玩得开心并学习新技能。 既然如此,理应如此,是时候说 “祝你学期愉快!”

尝试了一个 else 语句来增加特殊字符。


        // Declare a pointer to fopen function to access welcome file
        FILE *fp = fopen("/classes/cis158/cntwlc/welcome", "r");
        char fileName[100];
        char ch;

        int lineCount, charCount, wordCount, abcCount, numCount, speCount;


        lineCount = 0;
        wordCount = 0;
        charCount = 0;
        abcCount  = 0;
        numCount  = 0;
        speCount  = 0;


        gets(fileName);
        //fp = fopen(fileName, "r");


                while((ch = getc(fp)) != EOF) {
                        if(ch == '\n')
                                lineCount++;

                        if(ch == ' ' || ch == '\n')
                                wordCount++;

                        if(ch != ' ' || ch != '\n')
                                charCount++;

                        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                                abcCount++;

                        if(ch >= '0' && ch <= '9')
                                ++numCount;
                        else
                                speCount++;


                ch++;
                }
                /*if(charCount > 0) {
                        ++lineCount;
                        ++wordCount;
                }*/


        printf("---  Text Statistics:  ---\n\n");
        printf("Lines                %d\n", lineCount);
        printf("Words                %d\n", wordCount);
        printf("Characters           %d\n", charCount);
        printf("Alphabetic           %d\n", abcCount);
        printf("Digits               %d\n", numCount);
        printf("Special              %d\n", speCount);

getchar();
return 0;
        //printf("%20s", &userInput);

}// main

这些是预期结果:

--- 文本统计:--- 5号线 单词 37 人物 188 按字母顺序排列 139 数字 3 特别9

这是我运行程序时得到的结果:

--- 文本统计: ---

第 5 行 单词 37 人物 188 按字母顺序排列 139 数字 3 特价185

最佳答案

您的 else 只与数字的 if 匹配。既然发生了这种情况,任何非数字的字符都将计入您的特殊字符计数。

我建议如下:

// Declare a pointer to fopen function to access welcome file
        FILE *fp = fopen("/classes/cis158/cntwlc/welcome", "r");
        char fileName[100];
        char ch;

        int lineCount, charCount, wordCount, abcCount, numCount, speCount;


        lineCount = 0;
        wordCount = 0;
        charCount = 0;
        abcCount  = 0;
        numCount  = 0;
        speCount  = 0;


        gets(fileName);
        //fp = fopen(fileName, "r");


                while((ch = getc(fp)) != EOF) {
                        if(ch == '\n')
                                lineCount++;

                        if(ch == ' ' || ch == '\n')
                                wordCount++;

                        if(ch != ' ' || ch != '\n')
                                charCount++;

                        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                                abcCount++;

                        else if(ch >= '0' && ch <= '9')
                                ++numCount;
                        else if(ch != ' ' && ch != '\n')
                                speCount++;


                ch++;
                }
                /*if(charCount > 0) {
                        ++lineCount;
                        ++wordCount;
                }*/


        printf("---  Text Statistics:  ---\n\n");
        printf("Lines                %d\n", lineCount);
        printf("Words                %d\n", wordCount);
        printf("Characters           %d\n", charCount);
        printf("Alphabetic           %d\n", abcCount);
        printf("Digits               %d\n", numCount);
        printf("Special              %d\n", speCount);

getchar();
return 0;
        //printf("%20s", &userInput);

}// main

请注意,您的字数统计可能会出错,但我会将其留给您来解决。

关于c - 如何计算字符串中特殊字符的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55617026/

相关文章:

c++ - 使用适用于 iOS 的 openCV 拼接图像的问题

php - 如何替换已解码的 Non-breakable space (nbsp)

php - 如何将特殊/保留字符从 HTML 表单发布到 PHP 页面?

javascript - Google map v3 - 在标记标题中使用重音字符

c - 我对吗 ? -C表达式

c - strcpy 导致段错误

c# - 如何在 C# 中只获取字符串中的字母?

ruby - 从字符串中删除所有非字母、非数字字符?

c - C语言中的字符串指针

c - 解码C中的函数指针