Friday, December 16, 2011

ASCII VALUE

Introduction

ASCII (American Standard Code for Information Interchange)
Every character or number has there own ASCII value starting from 1 to 255. To see the ASCII value of a particular character through a C program we have to write a simple program.

char ch='A'; means it is declared as a character. We can print this 'A' that is value of ch on screen with the help of %c like

#include"stdio.h"
main()
{
    char ch='A';
    printf("ch is initialized with %c \n",ch);
}

To print its ASCII just replace %c with %d as shown below :

#include"stdio.h"
main()
{
    char ch='A';
    printf("ASCII Value of 'A' is %d \n",ch);
}

Or it can be written as :

#include"stdio.h"
main()
{
    char ch='A';
    printf("ASCII Value of %c is %d \n",ch,ch);
}

After a little modification the total program can be written as :

#include"stdio.h"
main()
{
    char ch;
    printf("Hit Any Character : ");
    scanf("%c",&ch);

        printf("ASCII Value of %c is %d \n",ch,ch);
}

Uppercase Lowercase :

There are 25 alphabets.
  • ASCII Value of "A" is 65 and "Z" is 90
  • ASCII Value of "a" is 97 and "z" is 122
So the difference between uppercase "A" and lowercase "a" is 32 and even for rest of the alphabets, with the help of this we can transfer uppercase into lowercase or lower to upper vise verse.
For example :

#include"stdio.h"
main()
{
    char ch;
    printf("Enter a Character from A to Z or a to z : ");
    scanf("%c",&ch);

    if(ch>='A'&&ch<='Z')
        printf("You Entered '%c' Uppercase into Lowercase : %c \n",ch,(ch+32));

    else if(ch>='a'&&ch<='z')
        printf("You Entered '%c' Lowercase into Uppercase : %c \n",ch,(ch-32));

    else
        printf("Your Entry was Wrong ! \n");
}

No comments:

Post a Comment

Followers