The number of times the C language reads characters in notepad.

Source: Internet
Author: User
Tags rewind

The number of times the C language reads characters in notepad.

Program instructions:

This program can calculate the number of times that a file named 1.txt appears.

However, you can only query single English letters and punctuation marks. Chinese characters are not supported.

Put a 1.txt file in the exeroot directory generated by this program,

You can query the number of times that the token appears in the 1.txt notepad.

By nebula

The test text is as follows:

The source code is as follows:

# Include <stdio. h> # include <windows. h> # include <string. h> # include <conio. h> int main () {FILE * fp; double I = 0, j = 0; char a, B;/* program description and interface settings, no impact on the program after deletion * // **********************/system ("color" ); printf ("\ n \ t \ tThis program can calculate the number of occurrences of/in a file named 1.txt: \ n "); printf ("\ n \ t this program only supports query of single English letters and punctuation marks, Chinese characters are not supported. \ N "); printf (" \ n \ t this program by Nebula, if you have any questions, please contact xingyun26845@gmail.com! \ N "); printf (" press any key to continue! \ N "); getch (); /* accept a single character ************//* to query the character */printf ("Enter the character to query: \ n "); scanf (" % c ", & B ); /* Statistics/number of times a file appears *********** /if (fp = fopen ("1.txt ", "r") = NULL) rewind (fp); while (! Feof (fp) {fscanf (fp, "% c", & a);/* read the characters in the file */if (a = '/') j ++;/* One count per read */} fclose (fp);/* close and save the file. */Printf ("\ n \ t/% appears in the file. 0f times \ n ", j ); /************************* // * count the number of times query characters appear in the file * // ****************************/if (fp = fopen ("1.txt ", "r") = NULL) rewind (fp);/* move the cursor to the beginning of the file */while (! Feof (fp) {fscanf (fp, "% c", & a); if (a = B) I ++;} fclose (fp ); *****************//******* */printf ("\ n \ tcc' appears in 1.txt. 0f times \ n ", B, I); printf (" \ t query character % c Probability: % f % \ n ", B, 100 * (I/j); printf ("press any key to exit the program! \ N "); getch (); return 0 ;}

Running result:


C language problems: Read the body from a text file and count the number of occurrences of each number and other characters

# Include "stdio. h"
# Include "conio. h"
# Include "math. h"
# Include "string. h"
Main ()

{Char a [] = "abcd hgh s1 &", ch;
Int I = 0, j = 0, k = 0, l = 0;
FILE * fp;
Fp = fopen ("d: \ liangnv.txt", "w ");
Fputs (a, fp );

Fclose (fp );
Fp = fopen ("d: \ liangnv.txt", "r ");
Ch = fgetc (fp );
While (ch! = EOF)
{If (ch> = 'A' & ch <= 'Z') I ++;
Else if (ch> = 'A' & ch <= 'Z') j ++;
Else if (ch> = '1' & ch <= '9') k ++;
Else l ++;

Ch = fgetc (fp );}
Fclose (fp );
Printf ("lowercase letter % d \ n", I );
Printf ("uppercase letter % d \ n", j );
Printf ("number \ n % d \ n", k );
Printf ("character % d \ n", l );
Getch ();
} Minute

Read a txt file in C language and calculate the number and location of each character in the file

When a node is not input, the node writes data to the file and counts the index of the linked list. The number of times of occurrence and location are unknown, make a global variable record or write it to the linked list. You can try this.
 

Related Article

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.