Freelancer

Alphabet or not

Checking whether an input is a character or not - C Programming Example

This C program example will  say whether a character is an alphabet or not.

C Programming Code

#include<stdio.h>
#include<conio.h>
main()
{
    char a1;
    printf("Please enter a character: \n");
    scanf("%c",&a1);
    if(a1>=97&&a1<=122)
        printf("%c is an alphabet",a1);
    else if(a1>=65&&a1<=90)
        printf("%c is an alphabet",a1);
    else
        printf("%c is not an alphabet",a1);
    getch();
}

Output of the code

C Programming Example
Whether an input is an alphabet or not 

C Programming Example
Whether an input is an alphabet or not 

Explanation of the code


·      #include<stdio.h>
#include<conio.h>
Ø This are called header file. A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header<stdio.h>.In the other hand  <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.
·       main()
Ø In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·       char i;
Ø char is a variable or data type.  A variable declared to be of type char can be used for storing character. Here we declare “a1” char type. So this variable can take character.
·       printf() function
Ø C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·       scanf() function
Ø This function is used to get input from the user of the program. scanf(“%f”,&r”) is used here. So, this program will read in a float value for “r” variable that the user enters on the keyboard (%f is for float values. As there is “&r”, the number user enters will be the value of “r” variable).
·       if else loop
Ø If the expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed. Here, we can see that in the brackets of first ifloop, there is a expression “a1>=97&&a1<=122” .The number assigned here are the ASCII code of the alphabet from a-z.So if the ASCII code of enter character are between 97-122, then the code within first if block will be executed. in the brackets of else if loop, there is a expression “a1>=65&&a1<=90” .The number assigned here are the ASCII code of the alphabet from A-Z. So if the ASCII code of enter character are between 65-90, then the code within else if block will be executed. If a character doesn’t meet the conditions of first two if loop, then the code within else block will be executed. 
·       getch() function
Ø getch() is used to hold the console(output) window on the screen after the whole program run is completed till the user enters a key from keyboard. This function is present in the header file called conio.h.

Final result

This program will show us the whether a character is an alphabet or not. What we need to do is, just input character.

No comments

Powered by Blogger.