C Tutorial/Data Type/char function
Версия от 14:21, 25 мая 2010; (обсуждение)
Using functions isdigit, isalpha, isalnum, and isxdigit
#include <stdio.h>
#include <ctype.h>
int main()
{
printf( "%s\n%s%s\n%s%s\n\n", "According to isdigit: ",
isdigit( "8" ) ? "8 is a " : "8 is not a ", "digit",
isdigit( "#" ) ? "# is a " : "# is not a ", "digit" );
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
"According to isalpha:",
isalpha( "A" ) ? "A is a " : "A is not a ", "letter",
isalpha( "b" ) ? "b is a " : "b is not a ", "letter",
isalpha( "&" ) ? "& is a " : "& is not a ", "letter",
isalpha( "4" ) ? "4 is a " : "4 is not a ", "letter" );
printf( "%s\n%s%s\n%s%s\n%s%s\n\n",
"According to isalnum:",
isalnum( "A" ) ? "A is a " : "A is not a ",
"digit or a letter",
isalnum( "8" ) ? "8 is a " : "8 is not a ",
"digit or a letter",
isalnum( "#" ) ? "# is a " : "# is not a ",
"digit or a letter" );
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n",
"According to isxdigit:",
isxdigit( "F" ) ? "F is a " : "F is not a ",
"hexadecimal digit",
isxdigit( "J" ) ? "J is a " : "J is not a ",
"hexadecimal digit",
isxdigit( "7" ) ? "7 is a " : "7 is not a ",
"hexadecimal digit",
isxdigit( "$" ) ? "$ is a " : "$ is not a ",
"hexadecimal digit",
isxdigit( "f" ) ? "f is a " : "f is not a ",
"hexadecimal digit" );
return 0;
}
According to isdigit: 8 is a digit # is not a digit According to isalpha: A is a letter b is a letter & is not a letter 4 is not a letter According to isalnum: A is a digit or a letter 8 is a digit or a letter # is not a digit or a letter According to isxdigit: F is a hexadecimal digit J is not a hexadecimal digit 7 is a hexadecimal digit $ is not a hexadecimal digit f is a hexadecimal digit
Using functions islower, isupper, tolower, toupper
#include <stdio.h>
#include <ctype.h>
int main()
{
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
"According to islower:",
islower( "p" ) ? "p is a " : "p is not a ",
"lowercase letter",
islower( "P" ) ? "P is a " : "P is not a ",
"lowercase letter",
islower( "5" ) ? "5 is a " : "5 is not a ",
"lowercase letter",
islower( "!" ) ? "! is a " : "! is not a ",
"lowercase letter" );
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
"According to isupper:",
isupper( "D" ) ? "D is an " : "D is not an ",
"uppercase letter",
isupper( "d" ) ? "d is an " : "d is not an ",
"uppercase letter",
isupper( "8" ) ? "8 is an " : "8 is not an ",
"uppercase letter",
isupper( "$" ) ? "$ is an " : "$ is not an ",
"uppercase letter" );
printf( "%s%c\n%s%c\n%s%c\n%s%c\n",
"u converted to uppercase is ", toupper( "u" ),
"7 converted to uppercase is ", toupper( "7" ),
"$ converted to uppercase is ", toupper( "$" ),
"L converted to lowercase is ", tolower( "L" ) );
return 0;
}
According to islower: p is a lowercase letter P is not a lowercase letter 5 is not a lowercase letter ! is not a lowercase letter According to isupper: D is an uppercase letter d is not an uppercase letter 8 is not an uppercase letter $ is not an uppercase letter u converted to uppercase is U 7 converted to uppercase is 7 $ converted to uppercase is $ L converted to lowercase is l
Using functions isspace, iscntrl, ispunct, isprint, isgraph
#include <stdio.h>
#include <ctype.h>
int main()
{
printf( "%s\n%s%s%s\n%s%s%s\n%s%s\n\n",
"According to isspace:",
"Newline", isspace( "\n" ) ? " is a " : " is not a ",
"whitespace character", "Horizontal tab",
isspace( "\t" ) ? " is a " : " is not a ",
"whitespace character",
isspace( "%" ) ? "% is a " : "% is not a ",
"whitespace character" );
printf( "%s\n%s%s%s\n%s%s\n\n", "According to iscntrl:",
"Newline", iscntrl( "\n" ) ? " is a " : " is not a ",
"control character", iscntrl( "$" ) ? "$ is a " :
"$ is not a ", "control character" );
printf( "%s\n%s%s\n%s%s\n%s%s\n\n",
"According to ispunct:",
ispunct( ";" ) ? "; is a " : "; is not a ",
"punctuation character",
ispunct( "Y" ) ? "Y is a " : "Y is not a ",
"punctuation character",
ispunct( "#" ) ? "# is a " : "# is not a ",
"punctuation character" );
printf( "%s\n%s%s\n%s%s%s\n\n", "According to isprint:",
isprint( "$" ) ? "$ is a " : "$ is not a ",
"printing character",
"Alert", isprint( "\a" ) ? " is a " : " is not a ",
"printing character" );
printf( "%s\n%s%s\n%s%s%s\n", "According to isgraph:",
isgraph( "Q" ) ? "Q is a " : "Q is not a ",
"printing character other than a space",
"Space", isgraph( " " ) ? " is a " : " is not a ",
"printing character other than a space" );
return 0;
}
According to isspace: Newline is a whitespace character Horizontal tab is a whitespace character % is not a whitespace character According to iscntrl: Newline is a control character $ is not a control character According to ispunct: ; is a punctuation character Y is not a punctuation character # is a punctuation character According to isprint: $ is a printing character Alert is not a printing character According to isgraph: Q is a printing character other than a space Space is not a printing character other than a space