RMIT Classification: Trusted
EEET2601 Engineering Computing 1
Week 6 - Strings
RMIT Classification: Trusted
Characters
• ASCII (American Standard Code for Information Interchange) is a 7-bit
code that uses integers [0, 127] to represent characters.
Note: Extended version is 8-bit code.
• The ASCII code of a character can be described by either an integer or
a pair of single quotes around the character
char ch = 65;
char ch = 'A'; // preferred
2
RMIT Classification: Trusted
ASCII Table
Note: all characters
in [ ] are control
characters.
3
RMIT Classification: Trusted
<ctype.h>
Function Description
int isalpha(int c) Checks whether the given character is alphabetic
int isdigit(int c) Checks whether the given character is decimal digit
S
int isalnum(int c) Checks whether the given character is alphanumeric U
M
int ispunct(int c) Checks whether the given character is a punctuation character (e.g. . > : ;) M
A
int isspace(int c) Checks whether the given character is a whitespace (e.g. blank space, \t, \n)
R
int iscntrl(int c) Checks whether the given character is a control character Y
int islower(int c) Checks whether the given character is alphabetic lowercase letter
int isupper(int c) Checks whether the given character is alphabetic uppercase letter
int tolower(int c) Converts a given letter to lowercase
int toupper(int c) Converts a given letter to uppercase
4
RMIT Classification: Trusted
Characters
// Print all ASCII characters
printf("%3s %10s %4s\n", "Dec", "Char", "Type"); else if (ispunct(i)) {
for (int i = 0; i <= 127; i++) { printf("%10s", "punct");
printf("%3d %10c", i, i); } else if (isspace(i)) {
if (isalpha(i)) { printf("%10s", "space");
printf("%10s", "letter"); } else if (iscntrl(i)) {
} else if (isdigit(i)) { printf("%10s", "control");
printf("%10s", "digit"); } else {
} printf("%10s", "other");
}
printf("\n");
}
5
RMIT Classification: Trusted
Special Characters
Escape ASCII Code
Represented Character
Sequence (Decimal)
\b Backspace 8
\t Tab (Horizontal) 9
\n New Line 10
\" Double Quote 34
\\ Backslash 92
printf("First line \tTAB \tTAB"
"\nSecond line \tTAB \tTAB \n");
Note: When a character preceded by backslash (\), it is called an escape sequence.
An escape sequence represents a special character inside a string instead of being interpreted as it is.
All escape sequences can be referred here: www.tutorialspoint.com/escape-sequences-in-c 6
RMIT Classification: Trusted
String
• A string in C is an array of characters ended with the null
character '\0', which has the ASCII value 0
char s1[ ] = {'H', 'e', 'l', 'l', 'o', '\0'};
• A string can also be described by a pair of double quotes
around a sequence of characters without specifying the null
character explicitly
char s2[ ] = "Hello"; // '\0' is auto inserted
7
RMIT Classification: Trusted
String
// Two equivalent ways to declare the same string
char s1[ ] = {'H', 'e', 'l', 'l', 'o', '\0'};
char s2[ ] = "Hello"; // '\0' is auto inserted
// Print characters as decimal integers and characters
printf("%5s %5s %5s %5s\n", "Dec", "Char", "Dec", "Char");
for (int i = 0; i < 6; i++) {
printf("%5d %5c %5d %5c \n", s1[i], s1[i], s2[i], s2[i]);
}
8
RMIT Classification: Trusted
I/O functions
Function Description
int getchar() Read a character from stdin. Return the character or EOF
(-1) on end of file or error.
int putchar(int char) Write a given character to stdout. Return the written
character or EOF on error.
int scanf(const char *format, ...) Read formatted input from stdin. Return the number of
items successfully read or a negative value for error.
int printf(const char *format, ...) Write formatted output to stdout. Return the number of
characters successfully written or a negative value for
error.
Note: putchar(), scanf(), and printf() can be called without storing the return values
9
RMIT Classification: Trusted
I/O functions
// Read a character from stdin // Read and write a string
printf("Enter a character: "); char name[20];
int char1 = getchar(); printf("Enter your first name: ");
scanf("%s", name);
printf("Your first name is %s\n", name);
// Write a character to stdout
putchar(char1);
//Print out EOF value
printf("EOF is %d\n", EOF);
10
RMIT Classification: Trusted
I/O functions
• What if you want to input your full name with some spaces?
// Read and write string with spaces
char fullname[20];
printf("Enter your name: ");
scanf("%[^\n]s", fullname);
printf("Your full name is %s\n", fullname);
[^\n]: take input until newline character is entered. Ref: link1 link2
11
RMIT Classification: Trusted
<string.h>
Function Description
unsigned long strlen(const char *s) Return the number of characters in s, up to but not including
the null character '\0'.
int strcmp(const char *s1, const char *s2) Compare s1 to s2. Return 0 if they are the same, negative
or positive if s1 < s2 or s1 > s2 lexicographically.
int strncmp(const char *s1, const char *s2, size_t Compares at most the first n bytes of s1 and s2. Return 0 if
n) they are the same, negative or positive if s1 < s2 or s1 > s2
lexicographically.
char *strcpy(const char *s1, const char *s2) Copy s2 to s1, then return s1. You must make sure that s1
has enough space to store the result.
char *strncpy(char *s1, const char *s2, size_t n) Copy up to n characters from s2 to s1, then return s1. You
must make sure that s1 has enough space to store the
result.
12
RMIT Classification: Trusted
<string.h>
Function Description
char *strcat(const char *s1, const char *s2) Append s2 into s1, then return s1. You must make sure that s1
has enough space to store the result.
char *strncat(char *s1, const char *s2, size_t Append up to n characters from s2 into s1, then return s1. You
n) must make sure that s1 has enough space to store the result.
char *strstr(const char *s1, const char *s2) Finds the first occurrence of the entire string s2 (not including
the terminating null character) which appears in the string s1.
Return NULL if s2 is not found in s1.
char *strchr(const char *str, int c) Searches for the first occurrence of the character c (an
unsigned char) in the string str.
char *strrchr(const char *str, int c) Searches for the last occurrence of the character c (an
unsigned char) in the string str.
char *strtok(char *str, const char *delim) Breaks string str into a series of tokens separated by delim.
13
RMIT Classification: Trusted
<string.h>
// Create strings // Compare two strings
char s1[] = "Apple"; printf("The result of %s compared to %s is %d\n",
char s2[] = "Pineapple"; s1, s2, strcmp(s1, s2));
char s1c[] = "Apple"; printf("The result of %s compared to %s is %d\n",
s2, s1, strcmp(s2, s1));
// Find out string's length printf("The result of %s compared to %s is %d\n",
printf("%s's length is %lu\n", s1, s1c, strcmp(s1, s1c));
s1, strlen(s1));
printf("%s's length is %lu\n", // Copy string
s2, strlen(s2)); char s3[6];
strcpy(s3, s1); //Fine
printf("s3 is %s\n", s3);
strcpy(s3, s2); // Why error?
printf("s3 is %s\n", s3);
14
RMIT Classification: Trusted
Array of strings
// Create an array of strings
char names[3][20] = {
"Roger Federer",
"David Beckham",
"Tiger Woods"
};
// Print them out
for (int i = 0; i < 3; i++) {
printf("%s \n", names[i]);
}
15
RMIT Classification: Trusted
Preprocessor Directives
▪ The C preprocessor executes before a program is compiled. Some actions it
performs are:
• The inclusion of other files into the file being compiled.
• Definition of symbolic constants and macros.
• Conditional compilation of program code.
▪ Preprocessor directives begin with #
RMIT Classification: Trusted
Preprocessor Directives
▪ #include <filename> //include standard library headers
▪ #include "filename" //include user-defined library headers
▪ #define identifier replacement-text //identifier will be replaced by replacement-text
Example: #define PI 3.14159
then in your code, you can use PI as a constant value (not variable), e.g. x = x * PI
▪ #if //Evaluates the expression/condition
▪ #else #elseif //Indicate alternative statements of an if directive
▪ #endif //Used to indicate the end of an if directive
RMIT Classification: Trusted
Exercise
Get three full names and a search keyword. Find and print the full names
containing the search keyword. Assume that each full name has maximum
50 characters. Here is a sample run:
Enter full name 1: Andrew John Smith
Enter full name 2: John Doe
Enter full name 3: Minh Van Nguyen
Enter a search keyword: oh
The matched names:
Andrew John Smith
John Doe
18
RMIT Classification: Trusted
References
1. Fresh2Refresh, C Programming Tutorial, 2020.
2. StudyTonight, C Programming Language Tutorial, 2020.
3. TutorialsPoint, C Standard Library, 2020.
4. Wikipedia, ASCII Table, 2010.
19