PS-Trainer C - Entwicklung
String-Funktionen: Zeichen ReadOnly
Homepage von PS-Trainer - C-Entwicklung - Strings - an PS-Trainer
PS-Trainer PS-Trainer

Strings & characters Overview
is, isw Routines These routines test characters for specified conditions.
memchr Finds characters in a buffer.
strchr, wcschr, _mbschr Find a character in a string.
strpbrk, wcspbrk, _mbspbrk Scan strings for characters in specified character sets.
strrchr, wcsrchr, _mbsrchr Scan a string for the last occurrence of a character.


is, isw Routines
These routines test characters for specified conditions.

isalnum iswalnum islower iswlower
isalpha iswalpha isprint iswprint
__isascii iswascii ispunct iswpunct
iscntrl iswcntrl isspace iswspace
__iscsym __iscsymf isupper iswupper
isdigit iswdigit isxdigit iswxdigit
isgraph iswgraph iswctype  

Remarks
These routines test characters for specified conditions.
The is routines produce meaningful results for any integer argument from –1 (EOF) to UCHAR_MAX (0xFF), inclusive. The expected argument type is int.

Warning For the is routines, passing an argument of type char may yield unpredictable results. An SBCS or MBCS single-byte character of type char with a value greater than 0x7F is negative. If a char is passed, the compiler may convert the value to a signed int or a signed long. This value may be sign-extended by the compiler, with unexpected results.

The isw routines produce meaningful results for any integer value from – 1 (WEOF) to 0xFFFF, inclusive. The wint_t data type is defined in WCHAR.H as an unsigned short; it can hold any wide character or the wide-character end-of-file (WEOF) value.

For each of the these routines, the result of the test for the specified condition depends on the LC_CTYPE category setting of the current locale; see setlocale for more information. In the "C" locale, the test conditions for the is routines are as follows:
isalnum Alphanumeric (A – Z, a – z, or 0 – 9)
isalpha Alphabetic (A – Z or a – z)
__isascii ASCII character (0x00 – 0x7F)
iscntrl Control character (0x00 – 0x1F or 0x7F)
__iscsym Letter, underscore, or digit
__iscsymf Letter or underscore
isdigit Decimal digit (0 – 9)
isgraph Printable character except space ( )
islower Lowercase letter (a – z)
isprint Printable character including space (0x20 – 0x7E)
ispunct Punctuation character
isspace White-space character (0x09 – 0x0D or 0x20)
isupper Uppercase letter (A – Z)
isxdigit Hexadecimal digit (A – F, a – f, or 0 – 9)
For the isw routines, the result of the test for the specified condition is independent of locale.
iswalpha Any wide character that is one of an implementation-defined set for which none of iswcntrl, iswdigit, iswpunct, or iswspace is true. iswalpha returns true only for wide characters for which iswupper or iswlower is true.
iswascii Wide-character representation of ASCII character (0x0000 – 0x007F).
iswcntrl Control wide character.
iswdigit Wide character corresponding to a decimal-digit character.
iswgraph Printable wide character except space wide character (L' ').
iswlower Lowercase letter, or one of implementation-defined set of wide characters for which none of iswcntrl, iswdigit, iswpunct, or iswspace is true. iswlower returns true only for wide characters that correspond to lowercase letters.
iswprint Printable wide character, including space wide character (L' ').
iswpunct Printable wide character that is neither space wide character (L' ') nor wide character for which iswalnum is true.
iswspace Wide character that corresponds to standard white-space character or is one of implementation-defined set of wide characters for which iswalnum is false. Standard white-space characters are: space (L' '), formfeed (L'\f'), newline (L'\n'), carriage return (L'\r'), horizontal tab (L'\t'), and vertical tab (L'\v').
iswupper Wide character that is uppercase or is one of an implementation-defined set of wide characters for which none of iswcntrl, iswdigit, iswpunct, or iswspace is true. iswupper returns true only for wide characters that correspond to uppercase characters.
iswxdigit Wide character that corresponds to a hexadecimal-digit character.

Subject: Character Classification Routines | Locale Routines
Keywords: See also setlocale, to Function Overview

Example
/* ISFAM.C: This program tests all characters between 0x0
* and 0x7F, then displays each character with abbreviations
* for the character-type codes that apply.

* Editor's note: the following output is significantly
* shortened with the use of ellipses. This full output
* is too long and repetitive.
*/

#include <stdio.h>
#include <ctype.h>

void main( void )
{
int ch;
for( ch = 0; ch <= 0x7F; ch++ )
{
printf( "%.2x ", ch );
printf( " %c", isprint( ch ) ? ch : '\0' );
printf( "%4s", isalnum( ch ) ? "AN" : "" );
printf( "%3s", isalpha( ch ) ? "A" : "" );
printf( "%3s", __isascii( ch ) ? "AS" : "" );
printf( "%3s", iscntrl( ch ) ? "C" : "" );
printf( "%3s", __iscsym( ch ) ? "CS " : "" );
printf( "%3s", __iscsymf( ch ) ? "CSF" : "" );
printf( "%3s", isdigit( ch ) ? "D" : "" );
printf( "%3s", isgraph( ch ) ? "G" : "" );
printf( "%3s", islower( ch ) ? "L" : "" );
printf( "%3s", ispunct( ch ) ? "PU" : "" );
printf( "%3s", isspace( ch ) ? "S" : "" );
printf( "%3s", isprint( ch ) ? "PR" : "" );
printf( "%3s", isupper( ch ) ? "U" : "" );
printf( "%3s", isxdigit( ch ) ? "X" : "" );
printf( "\n" );
}
}


Output
00
01
02
03
04
05
06
07
08
09
0a
0b
0c
0d
0e
0f
10
11
12
13
14
15
16
17
18
19
1a
1b
1c
1d
1e
1f
20 AS S PR
21 ! AS G PU PR
22 " AS G PU PR
23 # AS G PU PR
24 $ AS G PU PR
25 % AS G PU PR
26 & AS G PU PR
27 ' AS G PU PR
28 ( AS G PU PR
29 ) AS G PU PR
2a * AS G PU PR
2b + AS G PU PR
2c , AS G PU PR
2d - AS G PU PR
2e . AS G PU PR
2f / AS G PU PR
30 0 AN AS CS D G PR X
31 1 AN AS CS D G PR X
32 2 AN AS CS D G PR X
33 3 AN AS CS D G PR X
34 4 AN AS CS D G PR X
35 5 AN AS CS D G PR X
36 6 AN AS CS D G PR X
37 7 AN AS CS D G PR X
38 8 AN AS CS D G PR X
.
.
.
39 9 AN AS CS D G PR X
3a : AS G PU PR
3b ; AS G PU PR
3c < AS G PU PR
3d = AS G PU PR
.
.
.
3e > AS G PU PR
3f ? AS G PU PR
40 @ AS G PU PR
41 A AN A AS CS CSF G PR U X
42 B AN A AS CS CSF G PR U X
43 C AN A AS CS CSF G PR U X
44 D AN A AS CS CSF G PR U X
45 E AN A AS CS CSF G PR U X
46 F AN A AS CS CSF G PR U X
47 G AN A AS CS CSF G PR U
48 H AN A AS CS CSF G PR U
49 I AN A AS CS CSF G PR U
4a J AN A AS CS CSF G PR U
4b K AN A AS CS CSF G PR U
4c L AN A AS CS CSF G PR U
4d M AN A AS CS CSF G PR U
4e N AN A AS CS CSF G PR U
4f O AN A AS CS CSF G PR U
50 P AN A AS CS CSF G PR U
51 Q AN A AS CS CSF G PR U
52 R AN A AS CS CSF G PR U
53 S AN A AS CS CSF G PR U
54 T AN A AS CS CSF G PR U
55 U AN A AS CS CSF G PR U
56 V AN A AS CS CSF G PR U
57 W AN A AS CS CSF G PR U
58 X AN A AS CS CSF G PR U
59 Y AN A AS CS CSF G PR U
5a Z AN A AS CS CSF G PR U
5b [ AS G PU PR
5c \ AS G PU PR
5d ] AS G PU PR
5e ^ AS G PU PR
5f _ AS CS CSF G PU PR
60 ` AS G PU PR
61 a AN A AS CS CSF G L PR X
62 b AN A AS CS CSF G L PR X
63 c AN A AS CS CSF G L PR X
64 d AN A AS CS CSF G L PR X
65 e AN A AS CS CSF G L PR X
66 f AN A AS CS CSF G L PR X
67 g AN A AS CS CSF G L PR
68 h AN A AS CS CSF G L PR
69 i AN A AS CS CSF G L PR
6a j AN A AS CS CSF G L PR
6b k AN A AS CS CSF G L PR
6c l AN A AS CS CSF G L PR
6d m AN A AS CS CSF G L PR
6e n AN A AS CS CSF G L PR
6f o AN A AS CS CSF G L PR
70 p AN A AS CS CSF G L PR
71 q AN A AS CS CSF G L PR
72 r AN A AS CS CSF G L PR
73 s AN A AS CS CSF G L PR
74 t AN A AS CS CSF G L PR
75 u AN A AS CS CSF G L PR
76 v AN A AS CS CSF G L PR
77 w AN A AS CS CSF G L PR
78 x AN A AS CS CSF G L PR
79 y AN A AS CS CSF G L PR
7a z AN A AS CS CSF G L PR
7b { AS G PU PR
7c | AS G PU PR
7d } AS G PU PR
7e ~ AS G PU PR
7f


memchr
Finds characters in a buffer.
void *memchr( const void *buf, int c, size_t count );

Routine Required Header Compatibility
memchr <memory.h> or <string.h> ANSI, Win 95, Win NT

Remarks
The memchr function looks for the first occurrence of c in the first count bytes of buf. It stops when it finds c or when it has checked the first count bytes.

Subject: Buffer Manipulation Routines
Keywords: See also _memccpy, memcmp, memcpy, memset, strchr
Return Value
If successful, memchr returns a pointer to the first location of c in buf. Otherwise it returns NULL.

Parameters
buf Pointer to buffer
c Character to look for
count Number of characters to check

Example
/* MEMCHR.C */

#include <memory.h>
#include <stdio.h>

int ch = 'r';
char str[] = "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";

void main( void )
{
char *pdest;
int result;
printf( "String to be searched:\n\t\t%s\n", string );
printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );

printf( "Search char:\t%c\n", ch );
pdest = memchr( string, ch, sizeof( string ) );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\t\t%c found at position %d\n\n", ch, result );
else
printf( "Result:\t\t%c not found\n" );
}


Output
String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890

Search char: r
Result: r found at position 12


strchr, wcschr, _mbschr
Find a character in a string.
char *strchr( const char *string, int c );
wchar_t *wcschr( const wchar_t *string, wint_t c );
unsigned char *_mbschr( const unsigned char *string, unsigned int c );


Routine Required Header Compatibility
 strchr <string.h> ANSI, Win 95, Win NT
 wcschr <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbschr <mbstring.h> Win 95, Win NT

Remarks
The strchr function finds the first occurrence of c in string, or it returns NULL if c is not found. The null-terminating character is included in the search.
wcschr and _mbschr are wide-character and multibyte-character versions of strchr. The arguments and return value of wcschr are wide-character strings; those of _mbschr are multibyte-character strings. _mbschr recognizes multibyte-character sequences according to the multibyte code page currently in use. These three functions behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcschr strchr _mbschr wcschr

Subject: String Manipulation Routines
Keywords: See also strcspn, strncat, strncmp, strncpy, _strnicmp, strpbrk, strrchr, strstr
Return Value
Each of these functions returns a pointer to the first occurrence of c in string, or NULL if c is not found.

Parameters
string Null-terminated source string
c Character to be located

Example
/* STRCHR.C: This program illustrates searching for a character
* with strchr (search forward) or strrchr (search backward).
*/

#include <string.h>
#include <stdio.h>

int ch = 'r';

char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";

void main( void )
{
char *pdest;
int result;

printf( "String to be searched: \n\t\t%s\n", string );
printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
printf( "Search char:\t%c\n", ch );

/* Search forward. */
pdest = strchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tfirst %c found at position %d\n\n",
ch, result );
else
printf( "Result:\t%c not found\n" );

/* Search backward. */
pdest = strrchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tlast %c found at position %d\n\n", ch, result );
else
printf( "Result:\t%c not found\n" );
}


Output
String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890

Search char: r
Result: first r found at position 12

Result: last r found at position 30


strpbrk, wcspbrk, _mbspbrk
Scan strings for characters in specified character sets.
char *strpbrk( const char *string, const char *strCharSet );
wchar_t *wcspbrk( const wchar_t *string, const wchar_t *strCharSet );
unsigned char *_mbspbrk( const unsigned char*string, const unsigned char *strCharSet );


Routine Required Header Compatibility
 strpbrk <string.h> ANSI, Win 95, Win NT
 wcspbrk <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbspbrk <mbstring.h> Win 95, Win NT

Remarks
The strpbrk function returns a pointer to the first occurrence of a character in string that belongs to the set of characters in strCharSet. The search does not include the terminating null character.
wcspbrk and _mbspbrk are wide-character and multibyte-character versions of strpbrk. The arguments and return value of wcspbrk are wide-character strings; those of _mbspbrk are multibyte-character strings. These three functions behave identically otherwise. _mbspbrk is similar to _mbscspn except that _mbspbrk returns a pointer rather than a value of type size_t.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcspbrk strpbrk _mbspbrk wcspbrk

Subject: String Manipulation Routines
Keywords: See Also strcspn, strchr, strrchr
Return Value
Each of these functions returns a pointer to the first occurrence of any character from strCharSet in string, or a NULL pointer if the two string arguments have no characters in common.

Parameters
string Null-terminated, searched string
strCharSet Null-terminated character set

Example
/* STRPBRK.C */

#include <string.h>
#include <stdio.h>

void main( void )
{
char string[100] = "The 3 men and 2 boys ate 5 pigs\n";
char *result;
/* Return pointer to first 'a' or 'b' in "string" */
printf( "1: %s\n", string );
result = strpbrk( string, "0123456789" );
printf( "2: %s\n", result++ );
result = strpbrk( result, "0123456789" );
printf( "3: %s\n", result++ );
result = strpbrk( result, "0123456789" );
printf( "4: %s\n", result );
}


Output
1: The 3 men and 2 boys ate 5 pigs

2: 3 men and 2 boys ate 5 pigs

3: 2 boys ate 5 pigs

4: 5 pigs


strrchr, wcsrchr, _mbsrchr
Scan a string for the last occurrence of a character.
char *strrchr( const char *string, int c );
char *wcsrchr( const wchar_t *string, int c );
int _mbsrchr( const unsigned char *string, unsigned int c );


Routine Required Header Compatibility
 strrchr <string.h> ANSI, Win 95, Win NT
 wcsrchr <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbsrchr <mbstring.h> Win 95, Win NT

Remarks
The strrchr function finds the last occurrence of c (converted to char) in string. The search includes the terminating null character.
wcsrchr and _mbsrchr are wide-character and multibyte-character versions of strrchr. The arguments and return value of wcsrchr are wide-character strings; those of _mbsrchr are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcsrchr strrchr _mbsrchr wcsrchr

Subject: String Manipulation Routines
Keywords: See Also strchr, strcspn, _strnicmp, strpbrk, strspn
Return Value
Each of these functions returns a pointer to the last occurrence of c in string, or NULL if c is not found.

Parameters
string Null-terminated string to search
c Character to be located

Example
/* STRCHR.C: This program illustrates searching for a character
* with strchr (search forward) or strrchr (search backward).
*/

#include <string.h>
#include <stdio.h>

int ch = 'r';

char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";

void main( void )
{
char *pdest;
int result;

printf( "String to be searched: \n\t\t%s\n", string );
printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
printf( "Search char:\t%c\n", ch );

/* Search forward. */
pdest = strchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tfirst %c found at position %d\n\n",
ch, result );
else
printf( "Result:\t%c not found\n" );

/* Search backward. */
pdest = strrchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tlast %c found at position %d\n\n", ch, result );
else
printf( "Result:\t%c not found\n" );
}


Output
String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890

Search char: r
Result: first r found at position 12

Result: last r found at position 30


Homepage von PS-Trainer - C-Entwicklung - Strings - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1