简体   繁体   中英

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. Could somebody please point me in the right direction?

This is the string that I'm using:

Welcome to CIS158. C and Tux are working hard, are you? 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.


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

This is what I get when I run the program:

--- Text Statistics: ---

Lines 5 Words 37 Characters 188 Alphabetic 139 Digits 3 Special 185

Your else only matches the if about the numbers. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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