ConceptStep · learning platform for computer science

Traversing a string until \0

C programming · Lesson

C · lesson 22 / 27

Traversing a string until \0

Follow index i as it visits one character at a time and stops exactly when it reaches the terminator.

1 · Observe and predict

The concept

A C string does not carry its length. A loop can compute it by advancing while parola[i] differs from '\0'. When the test fails, i is the number of visible characters.

Mental model:

i=0 → parola[i] != '\0'? → i++ → repeat → at the end i = length

2 · Flowchart + String Memory

Follow the same algorithm as a flowchart

The flowchart shows the logical string value; the String Memory Explorer below shows the individual char cells and the \0 terminator actually required in C.

Loading flowchart…
3 · String Memory Explorer

See the string as memory cells

Each cell contains one char. The \0 terminator occupies a real cell and tells C functions where the string ends.

1 / 8

String ready in memory

The string has four visible characters and the terminator in cell 4.

4 · Flowchart ↔ C · Run in Page

Lab 1 — Run the example program

Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.

Traverse a string until \0 Start the flowchart to synchronize the code
                        1#include <stdio.h>
                        2 
                        3int main(void)
                        4{
                        5    char parola[] = "CIAO";
                        6    int i = 0;
                        7    while (parola[i] != '\0')
                        8    {
                        9        i = i + 1;
                        10    }
                        11    printf("%d\n", i);
                        12    return 0;
                        13}
Ready. Make a prediction before running.

                    
                

Lab 2 — Coding challenge: write your code — Length without strlen

Read one word up to 50 characters and print its length without strlen.

This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.

5 · Guided practice

Check your prediction

With parola = "CIAO", what value does i have when the while ends?

Challenge 1 / 5 · Understanding

Progress: 0 / 5 · correct: 0

Your competency map

What you need to know for Traversing a string until \0

ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.

Current topic Strong skill Review due Needs reinforcement To be mastered Optional support

Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.

GUIDED LEARNING PATHS

Want to choose the next skill to develop?