Third job (character, word count)

Source: Internet
Author: User

Title: reads the characters from the file, then counts the number of characters, and the number of words.

First open a file, the contents of the stored in the statistics, just start to judge whether it is a part of the word:

The code is as follows:

FILE *FP;//File Pointers    CharC//file character Read storagefp = fopen ("D:\\a\\a.txt","R"); if(fp = =NULL) {printf ("File open failed"); return 0; }     while(feof (fp)! =1) {C=fgetc (FP); if(Isalnum (c)! =0|| c = ='_'|| c = ='.') {Word (FP); Words[wordnum]=C; Wordnum++; }        Else if(c = =' ') {Space++; }        Else{character++; }    }        

If it is part of a word, then the function jumps into the word judgment function Word () and passes the file pointer in, and the character is stored in the array of Word storage,

The word () function is as follows:

intWord (Char*f) {    CharC;  while(Feof (f)! =1) {C=fgetc (f); if(Isalnum (c)! =0|| c = ='_'|| c = ='.') {character++; Words[wordnum]=C; Wordnum++; }        Else if(c = =' ') {Space++; Word++; Words[wordnum]=' '; Wordnum++; return 1; }        Else{Word++; Words[wordnum]=' '; Wordnum++; return 1; }    }}

When the file does not end, determine whether the pointer points to the character is part of the word, if the number of characters plus one, the character is stored in the word array; if it is a space, the number of space characters plus One, the number of characters plus one, the number of words plus one , the word array into a space character If it is another character, the number of characters plus one, the number of words plus one, a space character in the word array. Returns the main function.

Examples of output results:

Then the word output, the function is as follows:

intShow () {intA;  for(inti =0; I < wordnum-1; i++)    {        if(Words[i] = =' ') {printf ("\ n"); Continue; } printf ("%c", Words[i]); } printf ("Thanks for using");
}

When the element in the array is a space, the description is the end of a word, and if so, the output returns, and uses continue to jump out of the loop,

Examples of output results:

Homework Impressions:

This program is not too difficult, mainly to use the knowledge about the file, the beginning of the feeling is more annoying, because to write files, but when the completion of the program, look at their own program is very happy to run, the previous I do not want to use the file, sometimes with a large array, or the structure of the storage, But suddenly found that the code is very fun to write files, more interesting, but also a lot of practice, especially their favorite places.

Total Code:

#include <stdio.h>#include<ctype.h>#include<stdlib.h>intcharacter =0;//Number of charactersintWord =0;//number of wordsintSpace =0;//Number of spacesCharwords[ +];//Word StoreintWordnum =0;//array location for word storageintWord (Char*F);//Check if it's a wordintShow ();//Show WordsintMain () {intChoice//the selected inputFILE *FP;//File Pointers    CharC//file character Read storagefp = fopen ("D:\\a\\a.txt","R"); if(fp = =NULL) {printf ("File open failed"); return 0; }     while(feof (fp)! =1) {C=fgetc (FP); if(Isalnum (c)! =0|| c = ='_'|| c = ='.') {Word (FP); Words[wordnum]=C; Wordnum++; }        Else if(c = =' ') {Space++; }        Else{character++; }} printf ("The characters in the file, the words are counted as follows: \ n"); printf ("Number of words:%d, number of symbols:%d, number of spaces:%d\n", Word, character, space);    Fclose (FP); printf ("to view the word, press 1, or press any key"); scanf_s ("%d", &choice); if(Choice = =1) {show (); }    Else        return 1;}intWord (Char*f) {    CharC;  while(Feof (f)! =1) {C=fgetc (f); if(Isalnum (c)! =0|| c = ='_'|| c = ='.') {character++; Words[wordnum]=C; Wordnum++; }        Else if(c = =' ') {Space++; Word++; Words[wordnum]=' '; Wordnum++; return 1; }        Else{Word++; Words[wordnum]=' '; Wordnum++; return 1; }    }}intShow () {intA;  for(inti =0; I < wordnum-1; i++)    {        if(Words[i] = =' ') {printf ("\ n"); Continue; } printf ("%c", Words[i]); } printf ("Thanks for using");}

Third job (character, word count)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.