C program to find non repeating characters in a string

Finding non repeating characters in a string.

In this article we will learn how to code a C program to find non repeating characters in a string. Non repeating characters are those that are present in the string only once. To find non repeating characters in a string we will use one for loop to calculate the frequency of each character and print those characters that have frequency count one using another for loop.

C program to find non repeating characters in a string in C

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Initialize a for loop and terminate it at the end of string. 
  • This for loop will calculate the frequency of each character.
  • Print the characters having frequency one.

C Code For Finding Non Repeating Characters

#include<stdio.h> 
int main()
{
    //Initializing variables.
    char str[100]="prepinsta";
    int i;
    int freq[256] = {0};
    //Calculating frequency of each character.
    for(i = 0; str[i] != '\0'; i++)
    {
        freq[str[i]]++;
    }
printf("The non repeating characters are: ");
for(i = 0; i < 256; i++)
{
if(freq[i] == 1)//Finding uniques charcters and printing them.
{
printf(" %c ", i);
}
}
return 0;
}

output

The non repeating characters are: a e i n r s t

One comment on “C program to find non repeating characters in a string”


  • Eiwin

    #include
    #include
    #include

    int main()
    {
    //Initializing variables.
    char str1[100],str2[100];
    int first[26]={0}, second[26]={0}, c=0, flag=0;

    //Accepting inputs.
    printf(“Enter First String: “);
    fgets(str1, 100, stdin);
    printf(“Enter Second String: “);
    fgets(str2, 100, stdin);

    //Calculating frequencies of characters in first string.
    while(str1[c] != ‘\0’)
    {
    first[str1[c]-‘a’]++;
    c++;
    }

    c=0;
    //Calculating frequencies of characters in second string.
    while(str2[c] != ‘\0’)
    {
    second[str2[c]-‘a’]++;
    c++;
    }
    //Checking if frequencies of both the strings are same or not.
    for(c=0;c<26;c++)
    {
    if(first[c] != second[c])
    flag=1;
    }
    //Priting result.
    if(flag == 0)
    {
    printf("Strings are anagram.");
    }
    else
    {
    printf("Strings are not anagram.");
    }
    return 0;

    }