C/Data Type/Char Validation

Материал из C\C++ эксперт
Перейти к: навигация, поиск

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;
}