PART B · PRESCRIBED EXERCISE 02

Demonstrate four string.h functions

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 B02 · 01

Understand the task.

Aim: Demonstrate four string.h functions.

DEFINITION

String library. string.h declares operations on C strings and memory. The demonstrated string functions expect valid null-terminated strings and sufficiently large destination arrays.

Review the related theory in Unit 3.

LAB B02 · 02

Know what goes in and what comes out.

Two text lines, each at most 40 characters. Empty lines and spaces are allowed. The joined output directly concatenates the two strings without inserting a separator.

Sample input

BCA
Notes

Expected output

First length = 3
Second length = 5
Copy = BCA
Joined = BCANotes
Comparison = first comes before second

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

LAB B02 · 03

Plan the steps before writing code.

  1. Read the first and second lines with bounded fgets. Remove their newlines and validate lengths.
  2. Use strlen to measure each string.
  3. Use strcpy to copy the first into copy.
  4. Use strcpy and strcat to construct joined.
  5. Use the sign of strcmp to report lexical ordering or equality.

LAB B02 · 04

One complete program. Ready to run.

Demonstrate four string.h functionsmain.c
#include <stdio.h>
#include <string.h>
int main(void)
{
    char first[42], second[42], copy[41], joined[81];
    int comparison;
    if (fgets(first, sizeof first, stdin) == NULL) { puts("Enter two text lines."); return 1; }
    first[strcspn(first, "\n")] = '\0';
    if (strlen(first) > 40) { puts("Use at most 40 characters per line."); return 1; }
    if (fgets(second, sizeof second, stdin) == NULL) { puts("Enter two text lines."); return 1; }
    second[strcspn(second, "\n")] = '\0';
    if (strlen(second) > 40) { puts("Use at most 40 characters per line."); return 1; }
    strcpy(copy, first);
    strcpy(joined, first);
    strcat(joined, second);
    comparison = strcmp(first, second);
    printf("First length = %lu\nSecond length = %lu\n",
           (unsigned long)strlen(first), (unsigned long)strlen(second));
    printf("Copy = %s\nJoined = %s\n", copy, joined);
    printf("Comparison = %s\n", comparison < 0 ? "first comes before second" :
           comparison > 0 ? "first comes after second" : "equal strings");
    return 0;
}
EXPECTED OUTPUTFirst length = 3 Second length = 5 Copy = BCA Joined = BCANotes Comparison = first comes before second

Try this programDownload B02-string-functions.c
Compile on your own computer
gcc B02-string-functions.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 B02 · 05

Connect each statement to its purpose.

  • strlen returns size_t and excludes the null terminator. With these small bounded lengths, casting to unsigned long supports C90 printing via %lu.
  • strcpy copies the terminator too. copy has room for 40 characters plus one terminator.
  • strcat appends to an already terminated destination. joined has room for both 40-character inputs plus one terminator.
  • strcmp compares character values lexically. Only negative, zero or positive meaning is guaranteed, not an exact result of -1 or 1.
  • strcspn is used additionally to locate the newline for removal. No nonstandard strrev or strcmpi functions are required.

LAB B02 · 06

Follow the sample calculation.

Step or componentWhat happens
strlenBCA → 3; Notes → 5
strcpycopy becomes BCA
strcatjoined becomes BCANotes
strcmpReport whether first comes before, after, or equals second.

LAB B02 · 07

Check more than one input.

Try this input or caseExpected behaviour
Same / SameComparison = equal strings
Two empty linesLengths zero; copy and joined empty; equal strings.
Two 40-character linesJoined length 80 fits the 81-char destination.

Time and storage

O(L1 + L2) time for bounded input strings, and fixed arrays sized for the documented limits.

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 B02 · 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

First length = 3
Second length = 5
Copy = BCA
Joined = BCANotes
Comparison = first comes before second

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>
#include <string.h>
int main(void)
{
    char first[42], second[42], copy[41], joined[81];
    int comparison;
    if (fgets(first, sizeof first, stdin) == NULL) { puts("Enter two text lines."); return 1; }
    first[strcspn(first, "\n")] = '\0';
    if (strlen(first) > 40) { puts("Use at most 40 characters per line."); return 1; }
    if (fgets(second, sizeof second, stdin) == NULL) { puts("Enter two text lines."); return 1; }
    second[strcspn(second, "\n")] = '\0';
    if (strlen(second) > 40) { puts("Use at most 40 characters per line."); return 1; }
    strcpy(copy, first);
    strcpy(joined, first);
    strcat(joined, second);
    comparison = strcmp(first, second);
    printf("First length = %lu\nSecond length = %lu\n",
           (unsigned long)strlen(first), (unsigned long)strlen(second));
    printf("Copy = %s\nJoined = %s\n", copy, joined);
    printf("Comparison = %s\n", comparison < 0 ? "first comes before second" :
           comparison > 0 ? "first comes after second" : "equal strings");
    return 0;
}

LAB B02 · 09

Explain why the program works.

Why allocate one extra character?

Every destination string needs room for its null terminator.

Can strings be copied with copy = first?

No. These are arrays and cannot be assigned as whole arrays in C; strcpy copies their contents.

Does strcmp compare string lengths first?

No. It compares corresponding character values until a difference or terminator establishes the result.

LAB B02 · 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.