Check if a String has Repeating Characters in C
To check if a string contains repeating characters in C, you can use nested loops, frequency arrays, and bit manipulation.
1. Checking Repeating Characters using Nested Loops
In this example, we use nested loops to compare every character in the string with each subsequent character to detect duplicates.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello";
int len = strlen(str);
int found = 0;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (str[i] == str[j]) {
found = 1;
break;
}
}
if (found)
break;
}
if (found)
printf("Repeating characters found");
else
printf("No repeating characters found");
return 0;
}
Explanation:
- We declare a string
str
initialized to “hello”. - The function
strlen()
computes the length of the string, which is stored inlen
. - A nested
for
loop is used: the outer loop iterates through each character, and the inner loop compares the current character with all subsequent characters. - If a duplicate character is found, the variable
found
is set to 1 and both loops are exited usingbreak
. - Finally, we check the
found
flag to print the appropriate message.
Output:
Repeating characters found
2. Checking Repeating Characters using Frequency Array
In this example, we use an integer array to track the frequency of each character (assuming the ASCII character set) and check if any character appears more than once.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "world";
int freq[256] = {0};
int found = 0;
for (int i = 0; str[i] != '\0'; i++) {
freq[(int)str[i]]++;
if (freq[(int)str[i]] > 1) {
found = 1;
break;
}
}
if (found)
printf("Repeating characters found");
else
printf("No repeating characters found");
return 0;
}
Explanation:
- We declare a string
str
initialized to “world”. - An integer array
freq
of size 256 (covering all ASCII characters) is initialized to zero. - A
for
loop iterates through the string, and for each character, its corresponding frequency counter is incremented. - If the frequency of any character becomes greater than 1, the variable
found
is set to 1, and the loop exits. - The final
if
condition checks thefound
flag to print the appropriate message.
Output:
No repeating characters found
3. Checking Repeating Characters using Bit Manipulation (For Lowercase Letters)
In this example, we employ bit manipulation to efficiently check for repeating characters in a string that contains only lowercase letters (a-z).
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "abcdea";
int checker = 0;
int found = 0;
for (int i = 0; str[i] != '\0'; i++) {
int bitAtIndex = 1 << (str[i] - 'a');
if (checker & bitAtIndex) {
found = 1;
break;
}
checker |= bitAtIndex;
}
if (found)
printf("Repeating characters found");
else
printf("No repeating characters found");
return 0;
}
Explanation:
- We declare a string
str
initialized to “abcdea”. - The integer
checker
is used as a bit mask to record the occurrence of each character. - For each character in the string, we calculate
bitAtIndex
by left-shifting 1 by the difference between the character and ‘a’. - The
if
statement uses the bitwise AND operator (&
) to check if the corresponding bit is already set inchecker
. - If the bit is set, it indicates that the character has already been encountered, so
found
is set to 1 and the loop exits; otherwise, the bit is set inchecker
using the bitwise OR operator (|=
).
Output:
Repeating characters found
Conclusion
In this tutorial, we explored multiple methods to check if a string has repeating characters in C:
- Using nested loops to compare each character with every other character.
- Using a frequency array to count occurrences of each character.
- Using bit manipulation for an efficient solution with lowercase letters.