Thursday, May 26, 2011

Built in Character Function



There are a variety of functions provided for testing and mapping characters. The testing functions, which are described first, allow you to test if a character is of a particular type, such as alphabetic, upper or lower case, numeric, a control character, a punctuation mark, printable or not and so on. The character testing functions return an integer, either zero if the character supplied is not of the category specified, or non-zero if it was. The functions all take an integer argument, which should an int of the value of which should be representable as unsigned char.,

You can write your own routine for following function or you can use these. The following is a summary of all the character testing functions. The header <ctype.h> must be included before any of them is used.



Character function - 'ctype.h', header file should be included to use following character function .


_tolower(), tolower() - Translate characters to lowercase. tolower is a function that converts an integer ch (in the range EOF to 255) to its lowercase value (a to z; if it was uppercase, A to Z). All others are left unchanged. _tolower is a macro that does the same conversion as tolower, except that it should be used ONLY when ch is known to be uppercase (A-Z).

Declaration:

int tolower(int ch);
int _tolower(int ch);

Return Value:

      If ch is uppercase, _tolower and tolower return its converted value.

     If ch is not uppercase, tolower returns ch unchanged.

     _tolower's result is undefined.


_toupper() , toupper() - Translate characters to uppercase. รพ toupper is a function that converts an integer ch (in the range EOF to 255) to its uppercase value (A to Z; if it was lowercase, a to z). All others are left unchanged. _toupper is a macro that does the same conversion as toupper, except that it should be used only when ch is known to be lowercase (a to z).

Declaration:
       int toupper(int ch);
      int _toupper(int ch);

Return Value:

If ch is lowercase, _toupper and toupper return its converted value.

     If ch is not lowercase, toupper returns ch unchanged.

_toupper's result is undefined.

/* tolower / toupper example */

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

void main()
 {
       int len, i;
       char str[] = "THIS IS A STRING";
       clrscr();
       printf("Original>> ' %s ' \n",str);
       len = strlen(str);
       for (i=0; i<len; i++)
           {
                   str[i] = tolower(str[i]);
           }
       printf(" After tolower() >> ' %s \n",str);
       for (i=0; i<len; i++)
           {
               str[i] = toupper(str[i]);
           }
       printf(" Change back to using toupper() >> ' %s '\n",str);
       getch();
 }

Output

Original>> ' THIS IS A STRING '

After tolower() >> ' this is a string '

Change back to using toupper() >> ' THIS IS A STRING '



 isalnum(), isalpha(), isascii(),iscntrl(), isdigit(), isgraph(), islower(), isprint(), ispunct()(),isspace(), isupper(), isxdigit() - Character classification macros. These macros classify ASCII coded integer values by table lookup. Each macro is a predicate that returns a non-zero value for true and 0 for false. isascii() is defined on all integer values. The other  macros are defined only when isascii(c) is true or c is EOF.

Declarations:
            int isalnum(int c);
            int islower(int c);
            int isalpha(int c);
            int isprint(int c);
            int isascii(int c);
            int ispunct(int c);
            int iscntrl(int c);
            int isspace(int c);
            int isdigit(int c);
            int isupper(int c);
            int isgraph(int c);
            int isxdigit(int c);

Return Value:

These. macros return a non-zero value on success. For each macro, success is defined as follows:

isalpha(): c is a letter (A to Z or a to z)
isascii(): the low order byte of c is in the range 0 to 127 (0x00--0x7F)
iscntrl(): c is a delete character or ordinary control character (0x7F or 0x00 to 0x1F)
isdigit(): c is a digit (0 to 9)
isgraph(): c is a printing character, like isprint, except that a space character is excluded
islower(): c is a lowercase letter (a to z)
isprint(): c is a printing character (0x20 to 0x7E)
ispunct(): c is a punctuation character (iscntrl or isspace)
isspace(): c is a space, tab, carriage return, new line, vertical tab, or formfeed (0x09 to 0x0D, 0x20)
isupper(): c is an uppercase letter (A to Z)
isxdigit(): c is a hexadecimal digit (0 to 9, A to F, a to f)

Some Example

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

void main()
  {
        int len, i;
        char str[] = "8/F, Is My AD.";
        clrscr();
        len = strlen(str);
        printf("\nOriginal  String ' %s '\n",str);
        for (i=0; i<len; i++)
            {
                    if(islower(str[i]))
                    printf("\nCharacter in position %d is' %c ' is a lowercase letter\n",i,str[i]);
                    if(isupper(str[i]))
                        printf("\nCharacter in position %d is ' %c ' is a uppercase letter\n",i,str[i]);
                    if(isdigit(str[i]))
                        printf("\nCharacter in position %d is' %c ' is a digit\n",i,str[i]);
                    if(isspace(str[i]))
                        printf("\nCharacter in position %d is' %c ' is space\n ",i,str[i]);
                    if(ispunct(str[i]))
                        printf("\nCharacter in position %d is' %c ' is punctuation sign\n",i,str[i]);
            } 
        getch();
  }

Output

Original String ' 8/F, Is My AD. '

Character in position 0 is ' 8 ' is a digit

Character in position 1 is ' / 'is punctuation sign

Character in position 2 is ' F ' is an uppercase letter

Character in position 3 is ' , 'is punctuation sign

Character in position 4 is ' ' is space

Character in position 5 is ' I ' is an uppercase letter

Character in position 6 is ' s ' is a lowercase letter

Character in position 7 is ' ' is space

Character in position 8 is ' M ' is an uppercase letter

Character in position 9 is ' y ' is a lowercase letter

Character in position 10 is ' ' is space

Character in position 11 is ' A ' is an uppercase letter

Character in position 12 is ' D ' is an uppercase letter

Character in position 13 is ' . 'is punctuation sign


-->
Program to create your own Character Function


/* Check the type of a Character*/

#include <stdio.h>
#include <conio.h>

 void main()
   {
         char ch;
         int i;
         clrscr();
         printf("Enter A Character : ");
         fflush(stdin);
         ch=getche();
         if(ch>=65 && ch<=90 || ch>=97 && ch<=122)
                    printf("\n'%c' it is a Letter ",ch);
         if(ch>=65 && ch<=90)
                    printf("\n'%c' it is in UpperCase ",ch);
         else if(ch>=97 && ch<=122)
                    printf("\n'%c' it is in LowerCase ",ch);
         else if(ch==32)
                    printf("\n'%c' it is a Space ",ch);
         else if(ch>=48 && ch<=57)
                    printf("\n'%c' it is a Digit ",ch);
         else
                    printf("\n'%c' it is a Special Character ",ch);
         getch();
   }

Output

Enter A Character : R
'R' it is a Letter
'R' it is in UpperCase

Enter A Character : a
'a' it is a Letter
'a' it is in LowerCase

Enter A Character : 4
'4' it is a Digit

Enter A Character :
' ' it is a Space

Enter A Character : ?
'?' it is a Special Character




/* Character Conversion */
#include <stdio.h>
#include <conio.h>


 void main()
   {
         char ch;
         int i;
         clrscr();
         printf("Enter A Character : ");
         fflush(stdin);
         ch=getche();
         printf("\nOriginal Input '%c",ch);
         if(ch>=65 && ch<=90)
                    ch+=32;
         else if(ch>=97 && ch<=122)
                    ch-=32;
         printf("\nConverted to '%c' ",ch);
         getch();
   }


Output

Enter A Character : c
Original Input 'c
Converted to 'C'

Enter A Character : T
Original Input 'T
Converted to 't'



!!!While checking if you not sure about ASCII values then certainly this function will help you out.!!!