Character Type in C isalnum Check if character

  • Slides: 12
Download presentation
Character Type in C

Character Type in C

isalnum - Check if character is alphanumeric /* isalnum example */ #include <stdio. h>

isalnum - Check if character is alphanumeric /* isalnum example */ #include <stdio. h> #include <ctype. h> int main () { int i; char str[]="c 3 po. . . "; i=0; while (isalnum(str[i])) i++; printf ("The first %d characters are alphanumeric. n", i); return 0; }

isalpha - Check if character is alphabetic /* isalpha example */ #include <stdio. h>

isalpha - Check if character is alphabetic /* isalpha example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; char str[]="C++"; while (str[i]) { if (isalpha(str[i])) printf ("character %c is alphabeticn", str[i]); else printf ("character %c is not alphabeticn", str[i]); i++; } return 0; }

iscntrl - Check if character is a control character /* iscntrl example */ #include

iscntrl - Check if character is a control character /* iscntrl example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; char str[]="first line n second line n"; while (!iscntrl(str[i])) { putchar (str[i]); i++; } return 0; }

isdigit - Check if character is decimal digit /* isdigit example */ #include <stdio.

isdigit - Check if character is decimal digit /* isdigit example */ #include <stdio. h> #include <stdlib. h> #include <ctype. h> int main () { char str[]="1776 ad"; int year; if (isdigit(str[0])) { year = atoi (str); printf ("The year that followed %d was %d. n", year+1); } return 0; }

islower - Check if character is lowercase letter /* islower example */ #include <stdio.

islower - Check if character is lowercase letter /* islower example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; char str[]="Test String. n"; char c; while (str[i]) { c=str[i]; if (islower(c)) c=toupper(c); putchar (c); i++; } return 0; }

isprint - Check if character is printable /* isprint example */ #include <stdio. h>

isprint - Check if character is printable /* isprint example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; char str[]="first line n second line n"; while (isprint(str[i])) { putchar (str[i]); i++; } return 0; }

ispunct - Check if character is a punctuation character /* ispunct example */ #include

ispunct - Check if character is a punctuation character /* ispunct example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; int cx=0; char str[]="Hello, welcome!"; while (str[i]) { if (ispunct(str[i])) cx++; i++; } printf ("Sentence contains %d punctuation characters. n", cx); return 0; }

 • isspace - Check if character is a white-space /* isspace example */

• isspace - Check if character is a white-space /* isspace example */ #include <stdio. h> #include <ctype. h> int main () { int c; int i=0; char str[]="Example sentence to test isspacen"; while (str[i]) { c=str[i]; if (isspace(c)) c='n'; putchar (c); i++; } return 0; }

isupper - Check if character is uppercase letter /* isupper example */ #include <stdio.

isupper - Check if character is uppercase letter /* isupper example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; char str[]="Test String. n"; char c; while (str[i]) { c=str[i]; if (isupper(c)) c=tolower(c); putchar (c); i++; } return 0; }

tolower - Convert uppercase letter to lowercase /* tolower example */ #include <stdio. h>

tolower - Convert uppercase letter to lowercase /* tolower example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; char str[]="Test String. n"; char c; while (str[i]) { c=str[i]; putchar (tolower(c)); i++; } return 0; }

toupper - Convert lowercase letter to uppercase /* toupper example */ #include <stdio. h>

toupper - Convert lowercase letter to uppercase /* toupper example */ #include <stdio. h> #include <ctype. h> int main () { int i=0; char str[]="Test String. n"; char c; while (str[i]) { c=str[i]; putchar (toupper(c)); i++; } return 0; }