C Program to Find a Non-Repeating Character

C Program to Find a Non-Repeating Character

A school text exercise needs to first nonrepeating character while preserving the buffer’s valid characters and final terminator.

A first nonrepeating character occurs exactly once in the entire string and has the earliest position among such characters.

Before this exercise, review strings and their terminator and array bounds. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why first nonrepeating character uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Try aabb, where every character repeats.

First Nonrepeating Character: follow the data

  1. Given data

    A school text exercise needs to first nonrepeating character while preserving the buffer’s valid characters and final terminator.

  2. Rule

    A first nonrepeating character occurs exactly once in the entire string and has the earliest position among such characters.

  3. Observed result

    First unique: s

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

Count occurrences across the complete text, not only adjacent equal letters. Scanning candidate positions in order and stopping at the first count-one result implements the first requirement.

A C string ends at its first null character. Read only within its accessible storage and reserve a position for that terminator when producing a changed string. These examples use basic text bytes, not a Unicode grapheme-processing library.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
  const char text[] = "school";
  char found = '\0';
  size_t length = strlen(text);
  for (size_t i = 0; i < length; i++) {
    int occurrences = 0;
    for (size_t j = 0; j < length; j++) if (text[j] == text[i]) occurrences++;
    if (occurrences == 1) { found = text[i]; break; }
  }
  if (found != '\0') printf("First unique: %c\n", found); else puts("No unique character");
  return 0;
}

Expected output

First unique: s
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
  const char text[] = "school";
  char found = '\0';
  size_t length = strlen(text);
  for (size_t i = 0; i < length; i++) {
    int occurrences = 0;
    for (size_t j = 0; j < length; j++) if (text[j] == text[i]) occurrences++;
    if (occurrences == 1) { found = text[i]; break; }
  }
  if (found != '\0') printf("First unique: %c\n", found); else puts("No unique character");
  return 0;
}

The first candidate s occurs once in school; the repeated o does not affect that earlier valid result.

Compare the printed answer with the original text and the stated ordering or filtering rule. The presence of a terminator makes %s usable; it does not by itself make every earlier write fit.

Follow the worked calculation

  1. Start with the sample

    The first candidate s occurs once in school; the repeated o does not affect that earlier valid result.

  2. Apply the rule

    Compare the printed answer with the original text and the stated ordering or filtering rule. The presence of a terminator makes %s usable; it does not by itself make every earlier write fit.

  3. Check the result

    First unique: s

    First unique: s
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
  const char text[] = "school";
  char found = '\0';
  size_t length = strlen(text);
  for (size_t i = 0; i < length; i++) {
    int occurrences = 0;
    for (size_t j = 0; j < length; j++) if (text[j] == text[i]) occurrences++;
    if (occurrences == 1) { found = text[i]; break; }
  }
  if (found != '\0') printf("First unique: %c\n", found); else puts("No unique character");
  return 0;
}

Try aabb, where every character repeats.

Worked answer

The complete worked answer applies the same explicitly bounded operation to a small variation. Read its stored bytes and terminator separately from the visible output.

C17 · complete program

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
  const char text[] = "aabb";
  char found = '\0';
  size_t length = strlen(text);
  for (size_t i = 0; i < length; i++) {
    int occurrences = 0;
    for (size_t j = 0; j < length; j++) if (text[j] == text[i]) occurrences++;
    if (occurrences == 1) { found = text[i]; break; }
  }
  if (found != '\0') printf("First unique: %c\n", found); else puts("No unique character");
  return 0;
}

Expected output

No unique character
Run in C compiler →

Recognize the important boundaries

The simple scan is quadratic in text length. A frequency table can improve repeated scanning when the supported character domain is defined.

An untrusted input string needs a verified buffer bound and a valid terminator. strlen is not a safe way to prove arbitrary raw storage has a terminator.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why preserve a null terminator after changing string content?
Choose one answer. You can try again.

Answer and explanation

String operations need a boundary within accessible storage.

Correct. Without that marker, a %s operation can continue beyond the intended characters.

Use the idea independently

A first nonrepeating character occurs exactly once in the entire string and has the earliest position among such characters.

Continue with character-array storage.

Continue with bounded line reading.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Check a Palindrome String →