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
str
and a substringsubstr
that we want to search for. - The integer variable
count
is initialized to 0 to keep track of the number of occurrences. - The pointer
pos
is set to the beginning ofstr
and is used to locate occurrences ofsubstr
usingstrstr
. - Inside the
while
loop, we search forsubstr
instr
starting from the current position. - If
strstr
returns a non-NULL pointer, it means an occurrence is found, so we incrementcount
. - We then move the pointer
pos
forward by the length ofsubstr
to 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
str
and the substringsubstr
to be searched. - Variables
str_len
andsubstr_len
store the lengths ofstr
andsubstr
respectively. - The outer
for
loop iterates throughstr
from index 0 tostr_len - substr_len
to ensure enough characters remain for a possible match. - The inner
for
loop compares the substring with a segment ofstr
starting at indexi
. - If a mismatch is found, the inner loop breaks; otherwise, if
j
equalssubstr_len
, it indicates a complete match, andcount
is incremented. - Finally, the total count of repeated substrings is printed.
Output:
The substring "abc" appears 3 times.