PART B · PRESCRIBED EXERCISE 01

String length without a built-in function

Understand the algorithm. Run the code.
Explain the result with confidence.

Read the programOpen the playground
Aim & definitionStep-by-step algorithmWorked sampleExtra test cases3 viva answers

LAB B01 · 01

Understand the task.

Aim: String length without a built-in function.

DEFINITION

C string length. A C string is a sequence of characters terminated by the null character, \0. Its length is the number of stored characters before that terminator, excluding the terminator.

Review the related theory in Unit 3.

LAB B01 · 02

Know what goes in and what comes out.

One line containing at most 119 characters, including spaces. The input newline is excluded. An empty line is valid and has length zero.

Sample input

BCA notes

Expected output

Length = 9

Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.

LAB B01 · 03

Plan the steps before writing code.

  1. Read a bounded line using fgets.
  2. Start length at zero.
  3. Advance while the character is neither \0 nor the input newline.
  4. Reject an overlong line and replace any newline with \0.
  5. Display length.

LAB B01 · 04

One complete program. Ready to run.

String length without a built-in functionmain.c
#include <stdio.h>
int main(void)
{
    char text[121];
    int length = 0;
    if (fgets(text, sizeof text, stdin) == NULL) {
        puts("Enter a line of text."); return 1;
    }
    while (text[length] != '\0' && text[length] != '\n') length++;
    if (length > 119) { puts("Use at most 119 characters."); return 1; }
    text[length] = '\0';
    printf("Length = %d\n", length);
    return 0;
}
EXPECTED OUTPUTLength = 9

Try this programDownload B01-string-length.c
Compile on your own computer
gcc B01-string-length.c -o program

Run the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.

LAB B01 · 05

Connect each statement to its purpose.

  • The counting loop does not call strlen or another built-in length function.
  • Spaces are characters and contribute to the length.
  • fgets keeps the newline when it fits; the program deliberately stops before it.
  • This program counts char elements (bytes for ordinary single-byte text), not Unicode code points. The stored terminator is not counted.

LAB B01 · 06

Follow the sample calculation.

Step or componentWhat happens
BCA3 characters
Space1 additional character
notes5 additional characters
Total3 + 1 + 5 = 9

LAB B01 · 07

Check more than one input.

Try this input or caseExpected behaviour
Empty lineLength = 0
A BLength = 3
119 letters / 120 letters119 accepted / overlong line rejected.

Time and storage

O(L) time for a line of length L and a fixed 121-char input buffer.

Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.

LAB B01 · 08

Use the real C compiler.

Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.

Sample input (STDIN)

BCA notes

Expected output

Length = 9

Load the compiler when you’re ready to practise.

Your C workspace

Edit real C code and see real compiler output.

Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.

View / copy the selected source
#include <stdio.h>
int main(void)
{
    char text[121];
    int length = 0;
    if (fgets(text, sizeof text, stdin) == NULL) {
        puts("Enter a line of text."); return 1;
    }
    while (text[length] != '\0' && text[length] != '\n') length++;
    if (length > 119) { puts("Use at most 119 characters."); return 1; }
    text[length] = '\0';
    printf("Length = %d\n", length);
    return 0;
}

LAB B01 · 09

Explain why the program works.

Is \0 the same as the character 0?

No. \0 is the null terminator; the printable digit 0 is a different character.

Why avoid scanf("%s", text) here?

%s stops at whitespace, so it would not read a full line containing spaces.

Is the array capacity the string length?

No. Capacity includes available storage; length depends on the first null terminator.

LAB B01 · 10

Build the next skill.

Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.