To find the non repeating characters in a given string, we will first iterate the each characters of string and records the count. To records the counts we will have one more integer array which will store the frequency of each characters. At last will print only those character that occurs only one time.
How our program will behave?
- Our program will take one string as an input.
- We will have one extra integer array to keep the frequency of all characters of string.
- Now we will print only those characters that occurs only once in string.
C Program to print all non repeating characters in string
#include <stdio.h>
int main()
{
// Initializing variables
char str[100];
int i;
int freq[256] = {0}; // Frequency array for all ASCII characters
printf("=== C Program to Find All Non-Repeating Characters ===\n");
printf("Enter the string: ");
scanf("%[^\n]", str); // Read input with spaces
// Calculating the frequency of all characters
for(i = 0; str[i] != '\0'; i++) {
freq[(unsigned char)str[i]]++;
}
printf("Characters which appear only once:\n");
for(i = 0; str[i] != '\0'; i++) {
if(freq[(unsigned char)str[i]] == 1) {
printf("%c ", str[i]);
}
}
return 0;
}
Output
=== C Program to Find All Non-Repeating Characters ===
Enter the string: quescol website
Characters which appear only once:
q u c o l w b i t
What did you think?