PracticeDev/study_clang/c_rpc/dict2.c

53 lines
1.7 KiB
C
Raw Normal View History

2022-12-20 17:31:11 +08:00
/* dict2.c -- initw, insertw, deletew, lookupw */
#include <string.h>
#define MAXWORD 50 /* maximum length of a command or word */
#define DICTSIZ 100 /* maximum number of entries in dictionary. */
char dict[DICTSIZ][MAXWORD + 1]; /* storage for a dictionary of words */
int nwords = 0; /* number of words in the dictionary */
/* ------------------------------------------------------------------
* initw -- initialize the dictionary to contain no words at all
* ------------------------------------------------------------------ */
int initw(void)
{
nwords = 0;
return 1;
} /* end of initw */
/* ------------------------------------------------------------------
* insertw -- insert a word in the dictionary
* ------------------------------------------------------------------ */
int insertw(const char *word)
{
strcpy(dict[nwords], word);
nwords++;
return (nwords);
} /* end of insertw */
/* ------------------------------------------------------------------
* deletew -- delete a word from the dictionary
* ------------------------------------------------------------------ */
int deletew(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
nwords--;
strcpy(dict[i], dict[nwords]);
return (1);
}
} /* end of for */
return (0);
} /* end of deletew */
/* ------------------------------------------------------------------
* lookupw -- look up a word in the dictionary
* ------------------------------------------------------------------ */
int lookupw(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
return (1);
}
} /* end of for */
return (0);
} /* end of lookupw */