C Program for Testing, Reading and Writing Character Type
In the following post we can give example of C Program for Testing, Reading and Writing Character with step by step. The below code example shows the use of getchar function in an interactive environment. The program displays a question of YES/NO type to the user and reads the user’s response in a single character (Y or N). If the response is Y, it outputs the message
Example,
My name is Yaso
otherwise, outputs.
You are good for nothing
Note there is one line space between the input text and output message.
C Program for Reading Character from Keyboard Example
#include <stdio.h> main() { char answer; printf("Would you like to know my name?\n"); printf("Type Y for YES and N for NO: "); answer = getchar(); /* .... Reading a character...*/ if(answer == 'Y' || answer == 'y') printf("\n\nMy name is Yasp\n"); else printf("\n\nYou are good for nothing\n"); }
Output display as per below,
Would you like to know my name? Type Y for YES and N for NO: Y My name is Yaso Would you like to know my name? Type Y for YES and N for NO: n You are good for nothing
C Program for Testing Character Type with Example
This C Program requests the user to enter a character and displays a message on the screen telling the user whether the character is an alphabet or digit, or any other special character.
This program receives a character from the keyboard and tests whether it is a letter or digit and prints out a message accordingly. These tests are done with the help of the following functions:
- isalpha(character)
- isdigit(character)
For example, isalpha assumes a value non-zero (TRUE) if the argument character contains an alphabet; otherwise it assumes 0 (FALSE). Similar is the case with the function isdigit.
#include <stdio.h> #include <ctype.h> main() { char character; printf("Press any key\n"); character = getchar(); if (isalpha(character) > 0) printf("The character is a letter."); else if (isdigit (character) > 0) printf("The character is a digit."); else printf("The character is not alphanumeric."); }
Output Display as below,
Press any key h The character is a letter. Press any key 5 The character is a digit. Press any key * The character is not alphanumeric.
C Program for Writing Character To the Screen with Example
This C Program that reads a character from keyboard and then prints it in reverse case is given in below example. That is, if the input is upper case, the output will be lower case and vice versa.
The program uses three new functions: islower, toupper, and tolower. The function islower is a conditional function and takes the value TRUE if the argument is a lower case alphabet; otherwise takes the value FALSE. The function toupper converts the lower case argument into an upper case alphabet while the function tolower does the reverse.
#include <stdio.h> #include <ctype.h> main() { char alphabet; printf("Enter an alphabet"); putchar('\n'); /* move to next line */ alphabet = getchar(); if (islower(alphabet)) putchar(toupper(alphabet)); else putchar(tolower(alphabet)); }
Output Display as below,
Enter an alphabet a A Enter an alphabet Q q Enter an alphabet z Z
Let me know if you find any difficulty in understanding this C Program for Testing, Reading and Writing Character with example and I would be glad to explain it further.