Question
1. Problem A Character literals 1.1 Specification Write an ANSI-C program that reads input from the Standard input, and then outputs the processed information on
1. Problem A Character literals
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 integer and the numerical value that this character represents (how to get the numerical value of a character such as '3'?). If the character does not represent a digit, e.g., 'A', the program outputs that the character is not a digit.
1
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 it uses 0 to
represent false, and use non-zero integer to represent true. So, as a general practice in C, your function should return a non-zero number (usually 1) if c is a digit and returns 0
otherwise.
Note that you should NOT use if c=='0' c=='1' c=='2' c=='3' c=='9'. Instead, use the one-statement trick that we discussed in class.
- Note that in getting the numerical value of a digit character such as '3', you should NOT use if c=='0' c=='1' c=='2' c=='3' c=='9'. Instead, use the one-statement trick that we discussed in class.
- put the definition (implementation) of function isDigit after your main function.
- display the prompt and output as shown below.
1.3 Sample Inputs/Outputs: (ONE blank line between each interaction/iteration):
red 338 % gcc Wall lab2A.c -o lab2a
red 339 % lab2a
Enter an integer and a character separated by blank>12 c Character 'c' does not represent a digit
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
Enter an integer and a character separated by blank>120 ! Character '!' does not represent a digit
Enter an integer and a character separated by blank>-10000 a red 340 %
Submit your program by issuing submit 2031Z lab2 lab2A.c
2. Problem B character literals
2.1 Specification
Write an ANSI-C program that uses getchar and putchar to read from the Standard input, and outputs (duplicates) the characters to the Standard output. For each input character that is a lower-case letter (how to check?), converts it into the corresponding upper case letter in the output (how to convert?). The program continues to read and output until EOF is entered.
2
2.2 Implementation
You might want to start with the template file copy.c provided for lab1.
- name your program lab2B.c
- use getchar() and a loop to read characters.
- use putchar() to print the input characters on the standard output.
- do NOT use any other C library functions. Do your own checking and conversion. However, you should NOT use if c=='A' c=='B' c=='C' c=='D' c=='Z'. Instead, use the one-statement trick discussed in class. ch as islower(), toupper().
2.3 Sample Inputs/Outputs (from Standard input):
red 308 % gcc Wall o lab2b lab2B.c
red 309 % lab2b
Hello The World
HELLO THE WORLD
How Old Are You?
HOW OLD ARE YOU?
I am 22, and THANKs!
I AM 22, AND THANKS!
^D (press Ctrl and D)
red 310 %
2.4 Sample Inputs/Outputs (from redirected input file):
Using your favorite text editor, create a text file my_input.txt that contains hEllo
How Are You!
I Am Good and THAnKs!
red 311 % lab2b < my_input.txt
HELLO
HOW ARE YOU!
I AM GOOD AND THANKS!
red 312 %
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started