Vous êtes sur la page 1sur 6

Collage of Engineering (COE) EEEB114: Programming For Engineers

LAB 10: Characters and Strings


Student Name: Student ID: Section:

10.1) LEARNING OBJECTIVES


By the end of this lab session, you should be able to:
Differentiate between an integer digit and a character digit.
Understand the character handling and the standard input/output library.
Understand the string conversion and string manipulation functions.

10.2) PRE LAB ASSIGNMENT


Write down the function description for each function prototype.
Function Prototype Function Description

int islower (int c)

int isupper (int c)

int isalpha (int c)

int isdigit (int c)

int isalnum (int c)

Page 1 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

int isprint (int c)

int istoupper (int c)

int istolower (int c)

10.3) BACKGROUND
Character
1. Characters such as letters and punctuation are stored as integers according to a certain
numerical code, such as ASCII code that ranges from 0 to 127 and only requires 7 bits to
represent it.
2. Character constant is an integral value represented as a character in single quotes. a
represents the integer value of a. If ASCII code is used to represent characters, the ASCII
value for a is 97.
3. Using char to declare a character variable.
char c = a; // the same as char c = 97;
4. The header file ctype.h declares several functions to perform testing and manipulations
of characters.
5. Each function has one argument of character (int type). The value of argument shall be
representable as an unsigned char or shall equal to the value of the macro EOF, defined
in header file stdio.h. Each function returns a nonzero value for true or zero for false.

Page 2 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

Character Input and Output


Functions in <stdio.h>
1. intgetchar(void);
Reads the next character from the standard input.
2. intputchar(intc);
Print the character stored in c
3. intscanf(const char *, );
with conversion specifier c.
4. intprintf(char *, );
with conversion specifier c.
char c=a; printf(%c, c);

Character Handling
1. The header file ctype.h declares several functions to perform testing and manipulations
of characters.
2. Each function has one argument of character (inttype). The value of argument shall be
representable as an unsigned charor shall equal to the value of the macro EOF, defined
in header file stdio.h. Each function returns a nonzero value for true or zero for false.

Strings
1. Strings are series of characters treated as a single unit, it can include letters, digits, and
certain special characters (*,/,$)
2. String literal (string constant) -a sequence of multi-byte characters enclosed in double
quotes "Hello"
3. Declare a string as a character array or a variable of type char *
char str1[7] = {S, t, r, i, n, g, \0};
char str2[ ] = {S, t, r, i, n, g, \0};
char str3[ ] = String;
char *strPtr = String;
Page 3 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

String Input and Output


Functions in <stdio.h>
1. char *gets(char *s);
Inputs characters from the standard input into the array s until a newline or end-of-file
character is encountered. A terminating null character is appended to the array.
2. char *fgets(char *s, int n, FILE *stream);
Input maximum number of n-1 characters from the stream pointed to by stream into
the array s. No additional characters are read after a newline or end-of-file character is
encounted. A terminating null character is appended to the array.
3. int scanf(const char *, );
Reads a string with conversion specifier s.
4. int printf(const char *, );
Prints individual strings with conversion specifier s.
5. int puts(const char *s);
Prints the string s followed by a newline character.
6. int sprintf(char *s, const char *format, ...);
Equivalent to printf, except the output is stored in the array s instead of printing it on
the screen.
7. int sscanf(const char *s, const char *format,...);
Equivalent to scanf, except the input is read from the array s instead of reading it from
the keyboard.

Page 4 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

10.4) IN LAB ACTIVITIES


Activity A:
1. Write a program that will count the number of alphabets, digits and punctuations in a
sentence given by the user. (Hint: Use isalpha(), isdigit(), ispunct() and a repetition
structure.)

Example:
Enter your sentence: My name is (insert your name) and my student ID is (insert your
student ID)
Your sentence contains:
Alphabets: (Number of alphabets)
Digits: (Number of digits)
Punctuations: (Number of punctuations)

Activity B
1. Write a program that will count the length of string in a sentence given by the user.

Example:
Please input a string.
My name is (insert your name) and my student ID is (insert your student ID)
string len= 6
Attach your coding and output, write your discussions and conclusion.

Page 5 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

10.5) STATE YOUR LEARNING CURVE


Note: conclude what youve learned from this lab activity.

Page 6 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015

Vous aimerez peut-être aussi