简体   繁体   English

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

[英]How do I count the number of special characters in a string?

I'm writing a program that will count the number of lines, words, characters, digits, alphabetic letters, and special characters. 我正在编写一个程序,该程序将计算行,单词,字符,数字,字母和特殊字符的数量。 So far the program is almost complete, but the special characters are giving me trouble. 到目前为止,该程序几乎已经完成,但是特殊字符给我带来了麻烦。 I used a while loop with if statements to count these characters, with the special characters in an else statement. 我在if语句中使用while循环来计算这些字符,在else语句中使用特殊字符。 Could somebody please point me in the right direction? 有人可以指出正确的方向吗?

This is the string that I'm using: 这是我正在使用的字符串:

Welcome to CIS158. 欢迎来到CIS158。 C and Tux are working hard, are you? C和Tux正在努力工作,是吗? Hopefully you are having fun and learning a new skill. 希望您玩得开心,学习新技能。 That being the case, as it should, it is time to say "Have a Nice Semester!" 既然如此,是时候说“祝你学期快乐!”

Tried an else statement that would increment the special characters. 尝试了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

These are the expected results: 这些是预期的结果:

--- Text Statistics: --- Lines 5 Words 37 Characters 188 Alphabetic 139 Digits 3 Special 9 ---文字统计:---第5行单词37个字符188个字母139位3个特殊9

This is what I get when I run the program: 这是我运行程序时得到的:

--- Text Statistics: --- -文字统计:-

Lines 5 Words 37 Characters 188 Alphabetic 139 Digits 3 Special 185 行5字37个字符188个字母139位3个特殊185个

Your else only matches the if about the numbers. 您的else仅匹配数字的if。 Since that happens, any character that is not a number will count to your special character count. 既然发生了这种情况,那么不是数字的任何字符都将计入您的特殊字符数。

I suggest the following: 我建议以下内容:

// 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

Note that your word count is fallible, but I'll leave that for you to figure out. 请注意,您的字数统计有误,但我会留给您找出答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM