Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Patterns

Array

2D Array

String Function Example

Pointers

Recursion Function

Structure

Excersises

Others


C Program to Print All Unique Characters in a String


Write a C program that identifies and print all the unique characters present in a given string.

  • Reads a string from user input character by character until a newline is reached.
  • Uses an array freq[256] to count the frequency of each ASCII character as it's entered.
  • Traverses the string and checks which characters have a frequency of exactly one (i.e., are unique).
  • Prints only the characters that appear exactly once in the original string.
Example : pgm.c
#include <stdio.h>

int main() {
    char str[200];
    int freq[256] = {0}; // ASCII character frequency table
    int i = 0;

    printf("Enter a string: ");
    while ((str[i] = getchar()) != '\n') {
        freq[(unsigned char)str[i]]++; // Count frequency
        i++;
    }
    str[i] = '\0';

    printf("Unique characters: ");
    for (i = 0; str[i] != '\0'; i++) {
        if (freq[(unsigned char)str[i]] == 1) {
            printf("%c ", str[i]);
        }
    }

    printf("\n");
    return 0;
}

Output :

Enter a string: programming
Unique characters: p o a i n