Number of times that a keyword appears in the C Language

Source: Internet
Author: User

# Include <stdio. h> # include <ctype. h> # include <string. h> # define NKEYS (sizeof keytab/sizeof (struct key) struct key {char * word; int count ;};/* List of keywords (sort by dictionary) */struct key keytab [15] = {"abort", 0, "break", 0, "clock", 0, "define", 0, "echo", 0, "fgetc", 0, "get", 0, "help", 0, "insert", 0, "jump", 0, "kind", 0, "long ", 0, "malloc", 0, "null", 0, "operate", 0}; int binarysearch (char * word, struct key tab [], int n ); Int getword (char * word);/* <small exercise in The C programming language (second edition)> function: counts The number of times a keyword appears in The input text. */Int main () {char word [30]; int n; while (getword (word )! = 0 & strcmp (word, "quit ")! = 0) {if (n = binarysearch (word, keytab, NKEYS)> = 0); keytab [n]. count ++ ;}for (n = 0; n <NKEYS; n ++) {if (keytab [n]. count> 0) printf ("% s: % d \ n", keytab [n]. word, keytab [n]. count);} return 0;}/* get a word from the input */int getword (char * word) {char c; int I = 0; while (isspace (c = getchar (); while (1) {if (c! = '\ N' & c! = ''& C! = '\ T' & isalpha (c) word [I ++] = c; if (c =' \ n' | c = '') {word [I] = '\ 0'; return I;} c = getchar ();} return I;}/* binarysearch function: search for the word */int binarysearch (char * word, struct key tab [], int n) from tab [0] To tab [n] {int mid, l, h, flag; l = 0; h = n-1; while (l <= h) {mid = (l + h)/2; if (flag = strcmp (word, tab [mid]. word) <0) h = mid-1; else if (flag> 0) l = mid + 1; elsereturn mid;} return-1 ;}

 

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.