mmeemmcchhrr() -- String Function (libc)

Search a region of memory for a character
#iinncclluuddee <ssttrriinngg.hh>
cchhaarr *mmeemmcchhrr(_r_e_g_i_o_n, _c_h_a_r_a_c_t_e_r, _n)
cchhaarr *_r_e_g_i_o_n; iinntt _c_h_a_r_a_c_t_e_r; uunnssiiggnneedd iinntt _n;

mmeemmcchhrr()  searches  the first  _n  characters in  _r_e_g_i_o_n  for _c_h_a_r_a_c_t_e_r.  It
returns the address of _c_h_a_r_a_c_t_e_r if it is found, or NULL if it is not.

Unlike the  string-search function ssttrrcchhrr(), mmeemmcchhrr()  searches a region of
memory.  Therefore, it does not stop when it encounters a null character.

_E_x_a_m_p_l_e
The following example deals a random  hand of cards from a standard deck of
52.  The command  line takes one argument, which indicates  the size of the
hand you  want dealt.  It uses  an algorithm published by  Bob Floyd in the
September 1987 _C_o_m_m_u_n_i_c_a_t_i_o_n_s _o_f _t_h_e _A_C_M.

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DECK 52

main(argc, argv)
int argc; char *argv[];
{
    char deck[DECK], *fp;
    int  deckp, n, j, t;

    if(argc != 2 ||
       52 < (n = atoi(argv[1])) ||
       1 > n) {
           printf("usage: memchr n # where 0 < n < 53\n");
        exit(EXIT_FAILURE);
    }

    /* exercise rand() to make it more random */
    srand((unsigned int)time(NULL));
    for(j = 0; j < 100; j++)
        rand();

    deckp = 0;
    /* Bob Floyd's algorithm */
    for(j = DECK - n; j < DECK; j++) {
        t = rand() % (j + 1);
        if((fp = memchr(deck, t, deckp)) != NULL)
            *fp = (char)j;
        deck[deckp++] = (char)t;
    }

    for(t = j = 0; j < deckp; j++) {
        div_t card;

        card = div(deck[j], 13);
        t += printf("%c%c  ",
            /* note useful string addressing */
            "A23456789TJQK"[card.rem],
            "HCDS"[card.quot]);

        if(t > 50) {
            t = 0;
            putchar('\n');
        }
    }

    putchar('\n');
    return(EXIT_SUCCESS);
}

_S_e_e _A_l_s_o
lliibbcc, ssttrrcchhrr(), ssttrriinngg.hh, ssttrriinngg.hh
ANSI Standard, section 7.11.5.1
