summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/OR_PRACTICAL_C/14_01.c
blob: 43c1937e69e737ea52f9fe7347b29da6f6eeec0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/********************************************************
 * Database -- A very simple database program to        *
 *              look up names in a hardcoded list.      *
 *                                                      *
 * Usage:                                               *
 *      database                                        *
 *              Program will ask you for a name.        *
 *              Enter the name; it will tell you if     *
 *              it is the list.                         *
 *                                                      *
 *              A blank name terminates the program.    *
 ********************************************************/
#define STRING_LENGTH 80        /* Length of typical string */
#include <stdio.h>

main()
{
    char name[STRING_LENGTH];   /* a name to lookup */

    int lookup(char *); /* lookup a name */

    while (1) {
        (void)printf("Enter name: ");
        (void)fgets(name, sizeof(name), stdin);

        /* Check for blank name  */
        /* (remember 1 character for newline) */
        if (strlen(name) <= 1)
            break;

        /* Get rid of newline */
        name[strlen(name)-1] = '\0';

        if (lookup(name)) 
            (void)printf("%s is in the list\n", name);
        else
            (void)printf("%s is not in the list\n", name);
    }
    return (0);
}
/********************************************************
 * lookup -- lookup a name in a list                    *
 *                                                      *
 * Parameters                                           *
 *      name -- name to lookup                          *
 *                                                      *
 * Returns                                              *
 *      1 -- name in the list                           *
 *      0 -- name not in the list                       *
 ********************************************************/
int lookup(char *name)
{
    /* List of people in the database */
    /* Note: Last name is a NULL for end of list */
    static char *list[] = {
        "John",
        "Jim",
        "Jane",
        "Clyde",
        NULL
    };

    int index;          /* index into list */

    for (index = 0; list[index] != NULL; index++) {
        if (strcmp(list[index], name) == 0)
            return (1);
    }
    return (0);
}