Chapter 4-p - Example 4.1 The program in Fig.4.1 shows the...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 4.1 The program in Fig.4.1 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 My name is BUSY BEE otherwise, outputs. You are good for nothing Note there is one line space between the input text and output message. READING A CHARACTER FROM KEYBOARD Program #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 BUSY BEE\n"); else printf("\n\nYou are good for nothing\n"); } Output Would you like to know my name? Type Y for YES and N for NO: Y My name is BUSY BEE Would you like to know my name? Type Y for YES and N for NO: n
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
You are good for nothing Fig.4.1 Use of getchar function Example 4.2 The program of Fig.4.2 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 . TESTING CHARACTER TYPE Program: #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 Press any key h The character is a letter. Press any key 5
Background image of page 2
The character is a digit. Press any key * The character is not alphanumeric. ________________________________________________________________________ Fig.4.2 Program to test the characte r type Example 4.3 A program that reads a character from keyboard and then prints it in reverse case is given in Fig.4.3. That is, if the input is upper case, the output will be lower case and vice versa. The program uses three new functions:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/14/2010 for the course CS SS ZG653 taught by Professor Shanta during the Spring '09 term at Birla Institute of Technology & Science.

Page1 / 12

Chapter 4-p - Example 4.1 The program in Fig.4.1 shows the...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online