{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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
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.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern