C/Data Type/Char Validation
Версия от 14:20, 25 мая 2010; (обсуждение)
Содержание
- 1 Check a char: is it a alphanumeric char
- 2 Check a char: is it a control letter
- 3 Check a char: is it a letter
- 4 Is a char alphanumeric
- 5 Is a char punctuation
- 6 Is char a control char
- 7 Is char a letter
- 8 Is it a digit
- 9 is it a hexadecimal digit
- 10 Is it a lowercase char
- 11 Is it a space char
- 12 Is it a uppercase char
- 13 Is it printable
- 14 Is it printable: isgraph
Check a char: is it a alphanumeric char
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==".")
break;
if(isalnum(ch)) printf("%c is alphanumeric\n", ch);
}
return 0;
}
Check a char: is it a control letter
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==".")
break;
if(iscntrl(ch))
printf("%c is a control character\n", ch);
}
return 0;
}
Check a char: is it a letter
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==".")
break;
if(isalpha(ch))
printf("%c is a letter\n", ch);
}
return 0;
}
Is a char alphanumeric
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getc(stdin);
if(ch == ".") break;
if(isalnum(ch)) printf("%c is alphanumeric\n", ch);
}
return 0;
}
Is a char punctuation
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ispunct(ch)) printf("%c is punctuation\n", ch);
if(ch == ".") break;
}
return 0;
}
Is char a control char
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar( );
if(ch == ".") break;
if(iscntrl(ch)) printf("%c is a control char\n", ch);
}
return 0;
}
Is char a letter
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch == ".") break;
if(isalpha(ch)) printf("%c is a letter\n", ch);
}
return 0;
}
Is it a digit
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch == ".") break;
if(isdigit(ch)) printf("%c is a digit\n", ch);
}
return 0;
}
is it a hexadecimal digit
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch == ".") break;
if(isxdigit(ch)) printf("%c is hexadecimal digit\n", ch);
}
return 0;
}
Is it a lowercase char
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch == ".") break;
if(islower(ch)) printf("%c is lowercase\n", ch);
}
return 0;
}
Is it a space char
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(isspace(ch)) printf("%c is white space\n", ch);
if(ch == ".") break;
}
return 0;
}
Is it a uppercase char
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch == ".") break;
if(isupper(ch)) printf("%c is uppercase\n", ch);
}
return 0;
}
Is it printable
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(isprint(ch)) printf("%c is printable\n",ch);
if(ch == ".") break;
}
return 0;
}
Is it printable: isgraph
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(isgraph(ch)) printf("%c is printable\n", ch);
if(ch == ".") break;
}
return 0;
}