C language statistics on the number of source file keywords

Source: Internet
Author: User
# Include <stdlib. h> # include <stdio. h> # define max_word 128; # define max_len 100 typedef struct _ key {char * keyword; int cout;} keyword; // initialize keyword keytab [] = {"Auto ", 0, "break", 0, "case", 0, "char", 0, "const", 0, "continue", 0, "default", 0, "Define", 0, "unsigned", 0, "Void", 0, "volatile", 0, "while", 0}; // obtain the length of size_t getlen (void) {return (sizeof keytab/sizeof keytab [0]);} // print void printkey (keyword key [], Size_t Len) {int I; for (I = 0; I <Len; I ++) {printf ("% 4D % s \ n", key [I]. cout, key [I]. keyword) ;}}// obtain the word int getword (char * word, int Len, file * f) from the file {char * w = word; int C; // skip the space while (isspace (C = fgetc (f); If (C! = EOF) * w ++ = C; // If (! Isalpha (c) {* w = '\ 0'; return C ;}for (; -- Len> 0; W ++) {If (! Isalnum (* w = fgetc (F) {ungetc (* w, f); break ;}* W = '\ 0'; return C ;} // Binary Search int binsearch (keyword K [], char * word, int Len) {int low, high, mid, Pos; Low = 0; high = len-1; while (low <= high) {mid = (high + low)/2; // if the word is greater than the key [Mid]. the next cycle of keyword is in [Low ~ If (Pos = strcmp (word, K [Mid]. keyword) <0) {high = mid-1;} else if (Pos> 0) {LOW = Mid + 1;} else {return mid ;}} return-1;} // findkeyw. h // main. C # include <stdio. h> # include <stdlib. h> # include <string. h> # include "findkeyw. H "int main (INT argc, char * argv []) {extern keyword keytab []; size_t Len = getlen (); file * F; if (F = fopen ("findkeyw. H "," R ") {char word [max_len]; int Pos; while (getword (word, Ma X_len, f )! = EOF) {If (isalpha (word [0]) {If (Pos = binsearch (keytab, word, Len)> 0) {keytab [POS]. cout ++ ;}}fclose (f) ;}printkey (keytab, Len); System ("pause"); Return 0 ;}

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.