K & R exercise Question 6-1 count the number of times a keyword appears

Source: Internet
Author: User

This exercise is trained:

1. struct Array

2. Binary Search

3. pointer operation

----

It is not difficult, but it is very basic. I think it is very good. After I finish writing it to my blog, see K & R for the title. The implementation is as follows:

/* * Practice of struct array. K&R 6-1 * @author : wusuopubupt * @date   : 2014-09-18 */#include <stdio.h>#include <ctype.h>#include <string.h>#define MAXWORD 100typedef struct key_{char *word;int count;} key;key key_tab[] = {{"auto", 0},{"break", 0},{"case", 0},{"char", 0},{"const", 0},{"continue", 0},{"default", 0},{"for", 0},{"int", 0},{"void", 0},{"while", 0}};int getword(char *word, int n);int binary_search(key key_tab[], char *word, int n);int main() {int i;int n_keys = sizeof(key_tab) / sizeof(key_tab[0]);char word[MAXWORD];while(getword(word, MAXWORD) != EOF) {if(isalpha(word[0])) {if((i = binary_search(key_tab, word, n_keys)) >= 0) {key_tab[i].count++;}}}i = 0;while(i < n_keys) {printf("%s : %d\n", key_tab[i].word, key_tab[i].count);i++;}return 0;}int getword(char *word, int n) {int c;char *w = word;while(isspace(c = getchar())) {;}if(c != EOF) {*w++ = c;}if(!isalpha(c)) {*w = '\0';return c;}while(n > 0) {c = getchar();if(isalnum(c)) {*w++ = c;}else {break;}n--;}*w = '\0';return w[0];}int binary_search(key key_tab[], char *word, int n) {int low = 0;int high = n-1;int mid;int result;while(low <= high) {mid = (low+high) / 2;result = strcmp(word, key_tab[mid].word);if(result < 0) {high = mid-1;}else if(result > 0) {low = mid + 1;}else {return mid;}}return -1;}

GitHub: https://github.com/wusuopubupt/LearningC/blob/master/K%26R/chp6/keyword_count.c



K & R exercise Question 6-1 count the number of times a keyword appears

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.