Finding the first non-repeating character in a string is a classic problem in programming. It involves examining each character in the string and determining which character appears only once and comes first in order. This problem is a common exercise in coding interviews and helps you practice arrays, loops, and character manipulation in C.
In this tutorial, we will write a C program that identifies the first non-repeating character in a string. We will explore different approaches, including using count arrays and pointers. Each example will be explained step by step to ensure clarity.
Understanding the Problem
A string in C is an array of characters ending with a null terminator (\0
). The goal is to find the first character that appears only once in the string.
For example, given the string "programming"
, the first non-repeating character is 'p'
, since 'p'
occurs once and appears before all other unique characters.
To solve this problem, we can:
- Traverse the string and count the occurrences of each character.
- Traverse the string again to find the first character with a count of 1.
This approach ensures that we consider character order while efficiently tracking frequency.
Program 1: Using a Count Array
A simple way is to use an array of size 256 to store counts for ASCII characters.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count[256] = {0};
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// remove newline character if present
str[strcspn(str, "\n")] = '\0';
// Count occurrences
for (int i = 0; str[i] != '\0'; i++) {
count[(unsigned char)str[i]]++;
}
// Find first non-repeating character
char firstUnique = '\0';
for (int i = 0; str[i] != '\0'; i++) {
if (count[(unsigned char)str[i]] == 1) {
firstUnique = str[i];
break;
}
}
if (firstUnique != '\0') {
printf("The first non-repeating character is: %c\n", firstUnique);
} else {
printf("No non-repeating character found.\n");
}
return 0;
}
This program uses two traversals: first to count characters and second to identify the first unique one. It is efficient and easy to implement.
Program 2: Using Pointers
We can also use pointers to traverse the string and update the count array.
#include <stdio.h>
int main() {
char str[100], *ptr;
int count[256] = {0};
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline
for (ptr = str; *ptr != '\0'; ptr++) {
if (*ptr == '\n') {
*ptr = '\0';
break;
}
count[(unsigned char)*ptr]++;
}
// Find first non-repeating character
char firstUnique = '\0';
for (ptr = str; *ptr != '\0'; ptr++) {
if (count[(unsigned char)*ptr] == 1) {
firstUnique = *ptr;
break;
}
}
if (firstUnique != '\0') {
printf("The first non-repeating character is: %c\n", firstUnique);
} else {
printf("No non-repeating character found.\n");
}
return 0;
}
This pointer-based method avoids using array indices while maintaining the same logic. It also emphasizes memory access and character traversal in C.
Program 3: Case-Insensitive Version
Sometimes you may want to treat 'A'
and 'a'
as the same character. We can normalize characters using tolower()
from <ctype.h>
.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int count[256] = {0};
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == '\n') str[i] = '\0';
else count[(unsigned char)tolower(str[i])]++;
}
char firstUnique = '\0';
for (int i = 0; str[i] != '\0'; i++) {
if (count[(unsigned char)tolower(str[i])] == 1) {
firstUnique = str[i];
break;
}
}
if (firstUnique != '\0') {
printf("The first non-repeating character (case-insensitive) is: %c\n", firstUnique);
} else {
printf("No non-repeating character found.\n");
}
return 0;
}
This version counts characters without considering case, but prints the original character as it appeared in the string.
FAQs
1. Can this program handle spaces and punctuation?
Yes, all characters are counted, including spaces and punctuation.
2. How do I make the program case-insensitive?
Use the tolower()
or toupper()
function before counting and comparing characters.
3. What if all characters repeat?
The program will correctly indicate that there is no non-repeating character.
4. Is using a count array the most efficient method?
Yes, it is O(n) time complexity and very efficient for ASCII strings.
Conclusion
Finding the first non-repeating character is an essential problem for beginners in C programming. It teaches array handling, pointers, and character manipulation.
We explored three approaches: using a count array with loops, using pointers, and implementing a case-insensitive version. This exercise also prepares you for more advanced string-processing tasks like frequency analysis, anagrams, and text parsing.
References & Additional Resources
A curated collection of textbooks, tutorials, and documentation for learning string manipulation, character handling, and pointers in C.
- Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language, 2nd Edition, Prentice Hall, 1988 – The foundational text covering strings, pointers, arrays, and core C programming concepts.
- GeeksforGeeks: First Non-Repeating Character – Explains methods to find the first non-repeating character in a string with example code in C.
- Tutorialspoint: C Strings – Overview of string declaration, initialization, and common string operations in C.
- Cprogramming.com: Pointers in C – Beginner-friendly guide on pointers, including their use in string and array manipulation.
- cplusplus.com: ctype.h Functions – Reference for character-handling functions like
isalpha()
,isdigit()
, and case conversion utilities.