Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help solve this please: 1. Problem A Character literals (15 pts) 1.1 Specification Write an ANSI-C program that reads input from the Standard input, and

Help solve this please:

1. Problem A Character literals (15 pts)

1.1 Specification

Write an ANSI-C program that reads input from the Standard input, and then outputs the processed information on the Standard output.

Each user input contains an integer, followed by a blank and then a character. If the character does represent a digit, e.g., '3', (how to check?), then the program outputs the sum of the entered integer and the numerical value that this character represents (how to get the numerical value of a digit character such as '3'?). If the character does not represent a digit, but represents an alphabet letter, e.g., 'A', 'd', then the program outputs that the character is a letter. If the character is not a digit or letter, outputs that the character is others.

The program continues until the input integer is -10000 (and is followed by a blank and any one character).

1.2 Implementation

  • name your program lab2A.c
  • keep on reading and processing input, until an integer -10000 is read.
  • use scanf ("%d %c", ..) to read inputs.
  • define a Boolean function isDigit(char c) to determine if c represents a digit. We mentioned in class that ANSI-C does not have a type `boolean, instead ANSI-C uses 0 to represent false, and uses non-zero integer to represent true. So, as a general practice in C, your function should return a non-zero integer number (usually 1) if c is a digit and return 0 otherwise.

Note that you should NOT use library functions here (calling function is slower). Moreover, you should NOT write code like if (c==0 || c==1 || c==|| c==9) . Instead, use the shorter (one line) idiom discussed in class to examine if c is a digit char. Also for portability concerns, should NOT use a particular integer number in the idiom;

  • Note that in getting the numerical value of a digit character such as '3', you should NOT use if (c==0)elseif(c==1) elseif (c==..).elseif(c==9) . Instead, use the one-line idiom that we discussed in class. Also for portability concerns, should NOT use a particular integer number in the idiom;
  • define a Boolean function isLetter(char c) to determine if c represents an alphabet letter. Again, should NOT use library functions in this function, and should NOT use 50 if-else statements . Use the one line idiom instead.
  • put the definition (implementation) of function isDigit()and isLetter()after your main function.
  • call function isDigit()and isLetter()in main

1.3 Sample Inputs/Outputs: (ONE blank line between each interaction/iteration):

red 338 % gcc lab2A.c -o lab2a

red 339 % lab2a

Enter an integer and a character separated by blank: 12 c

Character 'c' represent a letter

Enter an integer and a character separated by blank: 12 9

Character '9' represents a digit. Sum of 12 and 9 is 21

Enter an integer and a character separated by blank: 100 8

Character '8' represents a digit. Sum of 100 and 8 is 108

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the best conclusion for Xbar Chart? UCL A X B C B A LCL

Answered: 1 week ago