1. 程式人生 > 其它 >【C語言】對輸入的字串中C關鍵詞的查詢統計

【C語言】對輸入的字串中C關鍵詞的查詢統計

完成對輸入的字串中C關鍵詞的查詢統計。
程式執行示例如下:
本程式將為您統計C語言的關鍵字的個數,請輸入,輸入end結束輸入:
if do while while do break goto helloworld end
您的輸入中C語言關鍵字出現的次數統計如下:
break     :      1
do        :      2
goto      :      1
if        :      1
while     :      2

輸入格式:
"本程式將為您統計C語言的關鍵字的個數,請輸入,輸入end結束輸入:\n"
輸出格式:
"您的輸入中C語言關鍵字出現的次數統計如下:\n"
"%-10s: %6d\n"

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#define MAX 32
struct Count {
    char* name;
    int count;
};

int main() {
    char s[10] = { 0 };
    struct Count arr[MAX] = {
        "auto", 0,
        "break",0,
        "case",0,
        "char",0,
        "const
", 0, "continue",0, "default",0, "do", 0, "double",0, "else",0, "enum",0, "float",0, "for",0, "goto",0, "if",0, "int",0, "long",0, "register",0, "while", 0, "reuturn",0, "short",0,
"signed",0, "sizeof",0, "static", 0, "struct",0, "switch",0, "typedef",0, "union",0, "unsigned",0, "void", 0 , "volatile",0, "while", 0, }; int i; printf("本程式將為您統計C語言的關鍵字的個數,請輸入,輸入end結束輸入:\n"); while (strcmp(s, "end") != 0) { scanf("%s", s); for (i = 0; i < MAX-1; i++) { if (strcmp(s, arr[i].name) == 0) { // strcmp--字串比較函式,如果兩個字串相等,返回值為0 arr[i].count++; } } } printf("您的輸入中C語言關鍵字出現的次數統計如下:\n"); for (i = 0; i < MAX-1; i++) { if (arr[i].count != 0) { printf("%-10s: %6d\n", arr[i].name, arr[i].count); } } }
方法一(推薦)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
#define MAXWORD 80
 
#define NKEYS (sizeof(keytab) / sizeof(struct key))
 
struct key
{                         
    char *word;
    int count;
}                          keytab[] =
{                         
    "auto", 0,
    "break", 0,
    "case", 0,
    "char", 0,
    "const", 0,
    "continue", 0,
    "default", 0,
    "do", 0,
    "double", 0,
    "else", 0,
    "enum", 0,
    "extern", 0,
    "float", 0,
    "for", 0,
    "goto", 0,
    "if", 0,
    "int", 0,
    "long", 0,
    "register", 0,
    "return", 0,
    "short", 0,
    "singed", 0,
    "sizeof", 0,
    "static", 0,
    "struct", 0,
    "switch", 0,
    "typedef", 0,
    "union", 0,
    "unsigned", 0,
    "void", 0,
    "volatile", 0,
    "while", 0
}                         ;
 
void getword(char *, int);
int binsearch(char *, struct key *, int);
 
int main(int argc, char *argv[])
{                         
    int n;
    char word[MAXWORD];
 
    printf("本程式將為您統計C語言的關鍵字的個數,請輸入,輸入end結束輸入:\n");
    do
    {                         
        getword(word, MAXWORD);
        if (isalpha(word[0]))
        {                         
            if ((n = binsearch(word, keytab, NKEYS)) >= 0)
            {                         
                keytab[n].count++; //找到則對應次數+1
            }
        }
 
    }
    while (strcmp(word, "end") != 0);
 
    printf("您的輸入中C語言關鍵字出現的次數統計如下:\n");
    for (n = 0; n < NKEYS; n++)
    {                         
        if (keytab[n].count > 0)
        {                         
            printf("%-10s: %6d\n", keytab[n].word, keytab[n].count);
        }
    }
 
    return 0;
}                         
 
/* 折半查詢:在tab[0]到tab[n-1]中查詢word */
int binsearch(char *word, struct key tab[], int n)
{                         
    int result;
    int low, high, mid;
 
    low = 0;
    high = n - 1;
    while (low <= high)
    {                         
        mid = (low + high) / 2;
        if ((result = strcmp(word, tab[mid].word)) < 0)
        {                         
            high = mid - 1;
        }
        else if (result > 0)
        {                         
            low = mid + 1;
        }
        else
        {                         
            return mid;
        }
    }
    return -1;
}                         
 
/* getword:從輸入中獲取某個單詞 */
void getword(char *word, int lim)
{                         
    int c;
    void ungetch(int);
    char *w = word;
 
    while (isspace(c = getchar()))
    {                         
    }
    if (c != EOF)
    {                         
        *w = c;
        w++;
    }
    if (!isalpha(c))
    {                         
        *w = '\0';
    }
    for ( ; --lim > 0; w++)
    {                         
        if (!isalnum(*w = getchar()))
        {                         
            //讀入的某個字元不是字母,則將它退還給輸入緩衝區
            ungetch(*w);
            break;
        }
    }
    *w = '\0';
} 
方法二


標頭檔案:<ctype.h>