Académique Documents
Professionnel Documents
Culture Documents
Page 1 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
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 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
Page 4 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
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
Page 6 of 6
Prepared By Dr. Nurul Asyikin Bte Mohamed Radzi May 2015