0% found this document useful (0 votes)
131 views

CPPS LAB 15 Solutions

The document provides instructions for a lab exercise on using character and string input/output functions in C programming. It includes two exercises: 1. Displaying a character array as a string by reading the ASCII values. Students are asked to write a program to continuously display the characters until the user presses 'Q'. 2. Reading a user's name within 25 characters and displaying it, without allowing '*' or '#' characters. Students are asked to write a program that prompts the user for input and checks for invalid characters or length.

Uploaded by

Joyce
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

CPPS LAB 15 Solutions

The document provides instructions for a lab exercise on using character and string input/output functions in C programming. It includes two exercises: 1. Displaying a character array as a string by reading the ASCII values. Students are asked to write a program to continuously display the characters until the user presses 'Q'. 2. Reading a user's name within 25 characters and displaying it, without allowing '*' or '#' characters. Students are asked to write a program that prompts the user for input and checks for invalid characters or length.

Uploaded by

Joyce
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Temasek Polytechnic

Diploma in
AEG/AEL/AMS/BME/BZE/CEN/CER/ELN/
GBS/IMT/INE/IFM/MET/MIE/MTN

Computer Programming for Problem Solving (ESE1006)


Laboratory 15: Strings for Information Interchange
Objectives: At the end of this session, students should be able to

i) Understand ASCII chart and character I/O


ii) Use character and string I/O library functions for communication with the PC
console.

Lab Exercise 1

a) Use the ASCII chart to complete the table for the following array declaration if the
contents are interpreted as ASCII characters.

int msg[]={103,110,105,110,114,111,77,32,100,111,111,71,0};

msg[10]

msg[12]
msg[11]
msg[0]

msg[1]

msg[2]

msg[3]

msg[4]

msg[5]

msg[6]

msg[7]

msg[8]

msg[9]

g n i n r o m SP d o o G NUL

b) Write a program to display the character from msg array above character by
character at least once. Your program will run until user press Q to quit.

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 1


1. Problem Design
 The objective is to use the correct character I/O functions in conjunction with
program control eg. Loop construct. Draw flow chart here.

2. Implementation
 Create a Win32
Console Project.
Initialize char [] msg
Add project to current
solution or create new
project and solution
named: Lab15Project at
Prompt and read location C:\Student
input
 Add C source file
named lab15.c
Y
 Implementation Steps
1. Declare and
initialize array
K=0 and other
variables.
Hints: How many
ways character
(data=msg[k]) array or string
k=0 can be initialized
in C?
N

k++ /*
putchar('\n')
Name: Goh Gwan Gei
Admin No.:1402944A
Class:Y PE11
*/
putchar(data)
#include <stdio.h>
#include <conio.h>
input 'Q'
int msg[]={103,110,105,110,114,111,77,32,100,111,111,71,0};
void main(void) N
{
char input;
int k,data;

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 2


2. Implement loop construct whether to continue program or not according to
the user input.
Hint: Use getch or _getch with no echo of input to screen

do{
printf("Press enter to continue or Q to quit program\n");
input = _getch();

3. Implement loop construct to display characters inside array.


Hint: Use putchar

printf("The integer value in char will be:\n");


for(k=0;(data=msg[k])!=0;k++)
{
putchar(data);
}
putchar('\n');
}while(input!='Q');
}

4. Your completed program resembles to code below.


CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 3
/*
Name: Goh Gwan Gei
Admin No.:1402944A
Class: PE11
/*

#include <stdio.h>
#include <conio.h>
int msg[]={103,110,105,110,114,111,77,32,100,111,111,71,0};
void main(void)
{
char input;
int k,data;
do{
printf("Press enter to continue or Q to quit program\n");
input = _getch();
printf("The integer value in char will be:\n");
for(k=0;(data=msg[k])!=0;k++)
{
putchar(data);
}
putchar('\n');
}while(input!='Q');
}

Press <Ctrl> <F5> to build and run the program.


Does the program display tally with your table? Yes

What is the message if you read it backwards? Good Morning

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 4


Lab Exercise 2
Write a program to read in the user’s name. User can type a maximum of 25 characters
including spaces.
User name should not contain ‘*’ or ‘#’ characters. If the user enters ‘*’ or ‘#’, program
displays a message to ask user to re-enter the name.
When user ends the entry with <enter> key, or the maximum input limit is reached the
name entered is displayed.

1. Problem specification
a. User’s name can consist of any character except ‘*’ and ‘#’.
b. User cannot key in more than 25 characters.
c. User get to see what they key in.

2. Problem Analysis
 What are the data inputs to the problem?

Maximum number of characters, error message, welcome message

 What data is known?

None

 What data is unknown and how is it acquired?

Name of user acquired through user input on keyboard

 What is the required output data and how is it displayed?

Name of user Displayed from a monitor console.

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 5


3. Problem Design
 Use a top-down approach to define a list of steps (algorithm) to solve the problem,
and use a flowchart to define the flow of the steps or statements.

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 6


Prompt Read Input

Input==’*’||Input==’#’

Y
Display ErrorMsg N

Name[j]=inp
ut

j++
j=0

Y
J<MAX&&input!=’\n’

4. Implementation
 Create a Win32 Console Project to implement your program.
Add project to current solution or create new project and solution named:
Lab15aProject at location C:\Student
 Add C source file named lab15a.c
CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 7
 Implementation Steps
1. Declare and initialize array and other variables.
Hints: Character array with maximum size, error message array

/*
Name: Goh Gwan Gei
Admin No.:1402944A
Class: PE11
/*

#include <stdio.h>
#include <conio.h>
#define MAX 25
char welcomeMsg[] = "Please enter your name: (do not include '*' or '#')";
char errorMsg[] = "You have entered a invalid character";

void main(void)
{
int j=0;
char input;
char name[MAX];//character array with Max number

puts(welcomeMsg);//display welcome message

2. Implement loop construct to read in user’s name character by character until user
press <enter> key or reach to maximum number of characters.
Hint: Look out for usage between getche vs getchar. To exit the program
check “less than 25 inputs & not <enter> key”.

else
{
name[j] = input;
j++;
}
} while (j < 25 && input != 13);//less than 25 inputs & not <enter> key

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 8


3. Check the validity of input data before the data is stored in array eg. Input
should not be ‘*’ or ‘#’ characters.
Hint: Implement selection construct and continue the loop
do
{
input = _getche();//get input data
if (input == '*' || input == '#')//check for invalid input
{
puts(errorMsg);//display errorMsg[]
j = 0;//reset j
continue;//continue at while()
}

4. Display the name array to user.


Hint: Do not forget to append end of string data. Use puts to print.

name[j] = 0;//append a NULL character to end of input


puts("Your name is: \n");
puts(name);//display user's name
}//end program

5. Your completed program resembles the code below

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 9


/*
Name: Goh Gwan Gei
Admin No.:1402944A
Class: PE11
/*

#include <stdio.h>
#include <conio.h>
#define MAX 25
char welcomeMsg[] = "Please enter your name: (do not include '*' or '#')";
char errorMsg[] = "You have entered a invalid character";

void main(void)
{
int j=0;
char input;
char name[MAX];//character array with Max number

puts(welcomeMsg);//display welcome message

do
{
input = _getche();//get input data
if (input == '*' || input == '#')//check for invalid input
{
puts(errorMsg);//display errorMsg[]
j = 0;//reset j
continue;//continue at while()
}
else
{
name[j] = input;
j++;
}
} while (j < 25 && input != 13);//less than 25 inputs & not <enter> key
name[j] = 0;//append a NULL character to end of input
puts("Your name is: \n");
puts(name);//display user's name
}//end program

5. Program Testing

1. Press <Ctrl> <F5> to build and run the program. Enter the required data. Does the
program display the correct answer? Yes it display the correct answer.

2. If the program does not display the correct expected answer, check for errors, make
the changes in the source file and re-run the program by pressing <Ctrl> <F5>.
Write down the errors and reasons during the process of getting your program to
execute correctly.

_____________________________________________________________

_____________________________________________________________

_____________________________________________________________

_________________________________________________________________

_________________________________________________________________

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 10


Facilitator’s Signature: ___________________________

CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 11

You might also like