Check if a String Contains Repeated Substrings in C
To check if a string contains repeated substrings in C, you can use various methods such as the strstr function for simple substring search or manual nested loops to count occurrences. Both approaches allow you to determine if a given substring appears more than once in the string.
Example 1: Using strstr Function
In this example, we will check if a given substring appears repeatedly in a string by using the strstr function. The strstr function finds the first occurrence of the substring, and we then continue the search from the next position to count subsequent occurrences.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello world, hello C programmers!";
char substr[] = "hello";
int count = 0;
// Pointer to store the result of strstr function
char *pos = str;
// Use strstr to search for the substring repeatedly
while ((pos = strstr(pos, substr)) != NULL) {
count++;
pos += strlen(substr); // Move pointer past the found substring
}
printf("The substring \"%s\" appears %d times.\\n", substr, count);
return 0;
}
Explanation:
- We declare a string
strand a substringsubstrthat we want to search for. - The integer variable
countis initialized to 0 to keep track of the number of occurrences. - The pointer
posis set to the beginning ofstrand is used to locate occurrences ofsubstrusingstrstr. - Inside the
whileloop, we search forsubstrinstrstarting from the current position. - If
strstrreturns a non-NULL pointer, it means an occurrence is found, so we incrementcount. - We then move the pointer
posforward by the length ofsubstrto continue searching for additional occurrences. - Finally, we print the total count of occurrences.
Output:
The substring "hello" appears 2 times.
Example 2: Using Nested Loops for Manual Substring Matching
In this example, we will manually check for repeated occurrences of a substring by iterating through the main string with nested loops. This approach does not use any library functions for substring searching, giving you a better understanding of the underlying process.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "abcabcabcd";
char substr[] = "abc";
int count = 0;
int str_len = strlen(str);
int substr_len = strlen(substr);
// Loop through the main string
for (int i = 0; i <= str_len - substr_len; i++) {
int j;
// Check if substring matches starting at index i
for (j = 0; j < substr_len; j++) {
if (str[i + j] != substr[j]) {
break;
}
}
// If full substring was found, increment count
if (j == substr_len) {
count++;
}
}
printf("The substring \"%s\" appears %d times.\\n", substr, count);
return 0;
}
Explanation:
- We declare the main string
strand the substringsubstrto be searched. - Variables
str_lenandsubstr_lenstore the lengths ofstrandsubstrrespectively. - The outer
forloop iterates throughstrfrom index 0 tostr_len - substr_lento ensure enough characters remain for a possible match. - The inner
forloop compares the substring with a segment ofstrstarting at indexi. - If a mismatch is found, the inner loop breaks; otherwise, if
jequalssubstr_len, it indicates a complete match, andcountis incremented. - Finally, the total count of repeated substrings is printed.
Output:
The substring "abc" appears 3 times.
