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:

  1. We declare a string str initialized to “hello”.
  2. The function strlen() computes the length of the string, which is stored in len.
  3. 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.
  4. If a duplicate character is found, the variable found is set to 1 and both loops are exited using break.
  5. 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:

  1. We declare a string str initialized to “world”.
  2. An integer array freq of size 256 (covering all ASCII characters) is initialized to zero.
  3. A for loop iterates through the string, and for each character, its corresponding frequency counter is incremented.
  4. If the frequency of any character becomes greater than 1, the variable found is set to 1, and the loop exits.
  5. The final if condition checks the found 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:

  1. We declare a string str initialized to “abcdea”.
  2. The integer checker is used as a bit mask to record the occurrence of each character.
  3. For each character in the string, we calculate bitAtIndex by left-shifting 1 by the difference between the character and ‘a’.
  4. The if statement uses the bitwise AND operator (&) to check if the corresponding bit is already set in checker.
  5. 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 in checker 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:

  1. Using nested loops to compare each character with every other character.
  2. Using a frequency array to count occurrences of each character.
  3. Using bit manipulation for an efficient solution with lowercase letters.