Strings

Or, arrays Pt 2

Revision Sessions

  • Week 4 Wednesday 25/06/2025 12PM-2PM - Bongo/Tabla Lab (K17 G07/G08)

    Sign up at: https://buytickets.at/comp1511unsw/1290763 (Access Code: "COMP1511", also linked on the forum)

Assignment 1

  • Releasing really soon
  • Watch the Assignment Walkthrough video
  • Submission in ~Week 7
  • Worth 20%

Arrays recap

  • A collection of data, all of the same type. (homogeneous)
  • We have a single identifier for the entire array
  • It is a random access data structure, meaning we can access any element in the array at any time

The array declaration syntax

int ice_cream_per_day[7];

Declare + initialise

^ Note you can only do this when you declare, not later!

^ Will initialise all elements to 0

Some corrections

^ Will create a 7-element array

^ Will create a 14-element array, with the first 7 elements then 7 0'd out

Accessing elements

Writing elements

Strings

Strings!

  • Strings are multi-character words
  • "Jake Renzella" -> is a string with 13 characters!
  • Strings are great! They are everywhere!

Bad news

C doesn't have a string data type :(

Good news

C has arrays! :)

An int array

A char array

We can build our own string type by using an array of chars!

Strings in C are char arrays

  • A collection of characters
  • C does know how to work with char[]s

New problem

How does C know where the string ends?

char name[MAX_STR] = {'J', 'a', 'k', 'e'};

The null terminator

  • Remember in C, we don't know when arrays end
  • We have to keep track of the length ourselves
  • We can't always do this with char[]...
  • Instead, we place a special character called the null terminator at the end of our character arrays \0

char[]

Notice the \0 at the end! This means that C will know when it reaches the end of the array

How to use strings in C

  • Because strings are character arrays, the type is char[]
  • There are two ways to declare a string, here's one:
char word[] = {'h', 'e', 'l', 'l', 'o', '\0'};

Anyone think that's annoying?

Strings are very common

So there are easier ways to use them:

char word[] = "hello";
  • This is exactly the same as the previous example
  • It includes the null terminator!

String literals

"Jake!"

  • uses double quotes " to wrap the string literal
  • single quote for characters!
  • Used to assign strings to char[] easily:

    char name[] = "Jake Renzella";

Using strings

  • printing: printf or fputs
  • scanning: fgets
  • Both included in <stdio.h>

fgets

  • Reads a string from the terminal
  • fgets(array[], length, stream)
    • array[] -> The array that the string will be stored
    • length -> The number of characters that can be read in
    • stream -> The origin of the string (we always use stdin)

fgets usage

Reading strings in a loop

  • We can read until CTRL+D is entered in the terminal by calling fgets in a loop
  • fgets() stops reading when either length-1 characters are read, newline character is read or an end of file is reached, whichever comes first

Reading strings in a loop

Printing strings

fputs(array[], stream)

  • array[] -> the character array to be printed
  • stream -> the location to print, always use stdout in COMP1511

Printing strings

^ Why doesn't fputs need the LENGTH, like fget?

Other useful string functions

  • strlen() -> gives us the length of the string (excluding the \0).
  • strcpy() -> copy the contents of one string to another
  • strcat() -> join one string to the end of another (concatenate)
  • strcmp() -> compare two strings
  • strchr() -> find the first occurrence of a character

    note: some of these may require #include <string.h>

Demo

Feedback