string read_string(string prompt)
int read_integer(string prompt)
line = read_string(prompt);
return convert_to_integer(line);
* @brief Calculate the total length of all the names in the array
* @param names the array of names
* @param size the number of names in the array
* @return int with the total length of all the names
int total_length(string names[], int size)
// Start the result at 0 - if there are no names the total length is 0
// for each element in the array
for(int i = 0; i < size; i++)
// Add its length to the result
result += length_of(name);
// Return the total length
* @brief Tests if an array contains a given name
* @param names the array of names to search
* @param size the number of names in the array
* @param name the name to search for
* @return true when the name is found (case insensitive)
* @return false when the name is not found
bool contains(string names[], int size, string name)
for(int i = 0; i < size; i++)
if( to_lowercase(names[i]) == to_lowercase(name) )
* @brief Returns the shortest name in the array
* @param names the array of names
* @param size the number of names in the array
* @return string the shortest name in the array
string shortest_name(string names[], int size)
// Return an empty string if the array is empty
// Assume the first name is the shortest...
// Search the rest of the array to find a shorter name
for(int i = 1; i < size; i++)
// Is the current name in the array shorter?
if ( length_of(min) > length_of(names[i]) )
// It is... so store it as the new shortest name
// Return the shortest name we found
// Declare a constant for the size of the array
// Declare an array of three strings
// The array is called names
// Create a variable to store the index of the array
// This can be used to control the loops below
// Assign values to the elements within the array
// Start i at the first index - index 0
while ( i < SIZE ) // loop while i is less than the number of elements
// Store in the i-th element of the array
// i changes each loop - so this will store in names[0], names[1], ...
names[i] = read_string("Enter a name: ");
i = i + 1; // increment i... could be written as i++
// Access elements in the array to write out names entered
// Use a for loop to combine the loop control
// - i is initialised to 0
// - the loop continues while i is less than SIZE
// - i is incremented each loop (at the end)
for(i = 0; i < SIZE; i++)
// Use the total_length function to calculate the total length of all the names
write_line("Total length of all names: " + to_string(total_length(names, SIZE)));
has_andrew = contains(names, SIZE, "andrew");
write_line("Contains Andrew");
write_line(shortest_name(names, SIZE));