Chapter Contents

Previous

Next
strscntb

strscntb



Build a Translate Table for Use by strscan

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <lcstring.h>

char *strscntb(char *area, const char *str, int val);


DESCRIPTION

strscntb builds a translate table that you use with the strscan function to scan a character string for the first occurrence of one of a set of characters. A translate table is an array containing 1 byte for each of the 256 EBCDIC characters. strscan scans its argument string for a character whose value in the translate table is not 0.

area is either the address of a 256-byte array, or NULL . If area is NULL , strscntb builds the translate table in a static area, whose address is returned. If area is not NULL , the table is built in the specified array.

str is a string of characters that are to be translated to the same value. The table byte corresponding to each character in str has the value specified by val , while all other characters have the value of !val (that is, 1 if val is 0, and 0 otherwise). For example, if str is "ab" and val is 0, bytes 129 and 130 (the EBCDIC decimal values for a and b) in the translate table have the value 0 and all other bytes (except for byte 0) have the value 1.

The table entry for the null character is a special case; this byte is always nonzero and forces strscan to terminate its scan at the end of the argument string.

When building the translate table with strscntb , you must consider how you will use strscan . If you are going to use strscan to search for the first occurrence of a character in str , val should be nonzero. If you want to search for the first character not in str , val should be 0.


RETURN VALUE

strscntb returns a pointer to the translate table. If area is NULL , this table may not be modified by the program.


CAUTION

If strscntb is called with a NULL area value, the table addressed by the return value is a static area. This area may be modified by the next call to any of these functions: strscntb , memscntb , memchr , strchr , strcspn , strspn , and strpbrk .


IMPLEMENTATION

strscntb is implemented as a built-in function. Inline code is generated if str is a string literal and val is an integer constant.


EXAMPLE

This example using strscntb verifies that each string in an array contains only alphabetic characters and English punctuation. Using strscan rather than strspn is more efficient because a translate table only needs to be built once:

#include <lcstring.h>
#include <stdio.h>

#define SIZE 500

char *strings[SIZE];           /* string to be tested for */
                               /* unacceptable characters */
int i;
char engtable[256];            /* translate table         */

   /* Build table to skip letters and punctuation.        */
main()
{
   strscntb(engtable, "abcdefghijklmnopqrstuvwxyz"
                      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                      " ,./?:;'\ !-", 0);

   for (i = 0; i < SIZE; i++) {

         /* If unacceptable character is found in         */
         /*  string before null, print error message.     */
      if (*strscan(strings[i], engtable)) {
         printf("String %d contains unacceptable character:\n",
                i);
      }
   }
}


RELATED FUNCTIONS

memscntb , strchr , strcspn , strscan , strspn


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.