Lecture - Strings
Lecture - Strings
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.
2
RMIT Classification: Trusted
ASCII Table
Note: all characters
in [ ] are control
characters.
3
RMIT Classification: Trusted
<ctype.h>
Function Description
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
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
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
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);
10
RMIT Classification: Trusted
I/O functions
• What if you want to input your full name with some spaces?
[^\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"
};
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
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
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