对输入的字符串中C关键词的查找统计。

本文介绍了一个简单的C语言程序,该程序能够统计用户输入的C语言关键字的数量,并使用折半查找算法来提高搜索效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
#define MAXWORD 80
 
#define NKEYS (sizeof(keytab) / sizeof(struct key))
 
structkey
{                        
    char*word;
    intcount;
}                          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
}                         ;
 
voidgetword(char*, int);
intbinsearch(char*, structkey *, int);
 
intmain(intargc, char*argv[])
{                        
    intn;
    charword[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);
        }
    }
 
    return0;
}                        
 
/* 折半查找:在tab[0]到tab[n-1]中查找word */
intbinsearch(char*word, structkey tab[], intn)
{                        
    intresult;
    intlow, 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;
        }
        elseif (result > 0)
        {                        
            low = mid + 1;
        }
        else
        {                        
            returnmid;
        }
    }
    return-1;
}                        
 
/* getword:从输入中获取某个单词 */
voidgetword(char*word, intlim)
{                        
    intc;
    voidungetch(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';
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值