Write a Program to print Non-repeating characters in a string

27 June 2022

write-a-program-to-print-nonrepeating-characters-in-a-string


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get a string as the input from the user and print the non-repeating characters in a string.

 

Input Non-repeating characters

 

Hello

Output

H e o

 

C Program

#include <stdio.h>

int main()

{

    char str[20];

    int i;

    int frequency[256] = {0};

    printf("Enter a string: ");

    gets(str);

    for(i = 0; str[i] != '\0'; i++)

    {

        frequency[str[i]]++;

    }

    printf("Non repeating characters: ");

    for(i = 0; i < 256; i++)

    {

        if(frequency[i] == 1)

        {

            printf("%c ", i);

        }

    }

    return 0;

}

 

C++ Program

#include <iostream>

#include<string.h>

using namespace std;

int main()

{

    char str[20];

    int i;

    int frequency[256] = {0};

    cout<<"Enter a string: ";

    gets(str);

    for(i = 0; str[i] != '\0'; i++)

    {

        frequency[str[i]]++;

    }

    cout<<"Non repeating characters: ";

    for(i = 0; i < 256; i++)

    {

        if(frequency[i] == 1)

        {

            printf("%c ", i);

        }

    }

    return 0;

}

 

Java

import java.util.Scanner;

public class Main

{

              public static void main(String[] args) {

                             Scanner sc = new Scanner (System.in);

        System.out.println ("Enter a string");

        String str = sc.next ();

        int frequency[] = new int[256];

        for (int i = 0; i < str.length (); i++)

            frequency[str.charAt(i)]++;

        System.out.println ("Non repeating characters are: ");

        for (int i = 0; i < 256; i++)

            if (frequency[i] == 1)

                          System.out.print ((char) i + " ");

              }

}

 

Python

Str = input('Enter a string: ')

for i in Str:

    count = 0

    for j in Str:

        if i == j:

            count+=1

        if count > 1:

            break

    if count == 1:

        print(i,end = " ")


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !