UoG-UESTC 2020 Page 1 of 9
Lab Session: Strings, Structures and Bit Manipulation
Exercise 1: Strings are Arrays of Characters
Background
You may recall that in Lab 1, we defined strings variables as:
char animal[100] = "cat";
As you may guess now, we were actually creating an array of characters.
You may have noticed that we can print out a string without needing to specify the total
number of characters:
char animal[100] = "ferocious kitten";
printf("The %s sleeps!", animal);
The above code will print out "The ferocious kitten sleeps", even though the
char array animal contains 100 chars. This is because the final character is a special
ascii-null character: '\0' (added automatically by the compiler in this case)
Technical details
Dealing with strings:
#include
#include // extra include!
int main() {
char animal[100] = "ferocious kitten";
// length of the string
int length = strlen(animal);
int i;
for (i=0; i