$30
EECS 2031 Page 1 of 4
LAB 3 ─ Arrays, Types and Operators
Problem A
A.1 Specification
Write a C program to input a line of characters and store the input characters in an array. Reverse the
order of the input characters and display the reversed string on the standard output using printf.
A.2 Implementation
• The program is named lab3a.c. Use the given template lab3a.c and fill in your code.
• You are given an array of characters of size MAX_SIZE where MAX_SIZE = 100. The array is
named my_strg.
• Use getchar and a loop to read a line of characters, and store the input characters into array
my_strg. The loop terminates when a new line character ‘\n’ is entered. The new line character
‘\n’ is NOT part of the line (i.e., discard the new line character ‘\n’).
• Reverse the order of the input characters stored in array my_strg.
• Display on the standard output the reversed string using the printf statement as follows:
printf( "%s\n", my_strg );
A.3 Sample Inputs/Outputs
indigo 352 % lab3a
Hello, world!
!dlrow ,olleH
indigo 353 % lab3a
Welcome to CSE2031.
.1302ESC ot emocleW
indigo 354 % lab3a
A
A
indigo 355 % lab3a
123
EECS 2031 Page 2 of 4
321
Problem B
B.1 Specification
Write a C program to input an octal number in the form of a line of characters and store the input
characters in an array. Convert the octal number to a decimal integer and display the decimal integer on
the standard output using printf.
B.2 Implementation
• The program is named lab3b.c. Use the given template lab3b.c and fill in your code.
• You are given an array of characters of size MAX_SIZE where MAX_SIZE = 100. The array is
named my_strg.
• Use getchar and a loop to read an octal number in the form of a line of characters, and store the
input characters into array my_strg. The loop terminates when a new line character ‘\n’ is
entered. The new line character ‘\n’ is NOT part of the line (i.e., discard the new line character
‘\n’).
• Convert the octal number stored in array my_strg to a decimal integer.
• Display on the standard output the decimal integer using the printf statement as follows:
printf( "%d\n", my_int );
• If the input string does not contain a valid octal number, display on the standard output the error
message ”Error: not an octal number”.
B.3 Sample Inputs/Outputs
indigo 356 % lab3b
12
10
indigo 357 % lab3b
340
224
indigo 358 % lab3b
EECS 2031 Page 3 of 4
-340
-224
indigo 359 % lab3b
5
5
indigo 359 % lab3b
29
Error: not an octal number
indigo 360 % lab3b
abc
Error: not an octal number
Problem C
Repeat Problem B, except that the input now is a line of characters containing a hexadecimal number.
The hexadecimal digits ‘A’ to ‘F’ can be in upper case or lower case.
The program is named lab3c.c. Use the given template lab3c.c and fill in your code.
Sample Inputs/Outputs
indigo 361 % lab3c
8
8
indigo 362 % lab3c
10
16
indigo 363 % lab3c
-920
-2336
EECS 2031 Page 4 of 4
indigo 364 % lab3c
DE
222
indigo 365 % lab3c
1bc6
7110
indigo 364 % lab3c
2R3
Error: not a hexadecimal number
Common Notes
All submitted files should contain the following header:
/***************************************
* EECS2031 – Lab 3
* Filename: Name of file
* Author: Last name, first name
* Email: Your preferred email address
* EECS login ID: Your EECS login ID
****************************************/
In addition, all programs should follow the following guidelines:
• Include the stdio.h library in the header of your .c files.
• Use printf to print text and outputs according to the required formats.
• End each output result with a new line character ‘\n’.
• Do not use any C library functions except getchar(), putchar(), scanf() and
printf().
• Assume that the input strings are shorter than 100 characters and the resulting decimal numbers
are small enough to be stored in an integer variable.