I wrote this C code that can find the number of occurrences of a substring in a string. The problem is it overlaps meaning if I search for the letters pp in a string that has ppp the answer will be 2 occurrences instead of 1. I need help stopping this overlapping.
Description:
• This command should search for the given substring in string and then
return the number of occurrences of this substring if found or zero if not
found.
• <string> is sample statement
• <substring> contains a character or more that we need to find the number
of its occurrences in <string>
Notes:
• The given string contains multiple words separated by underscore; i.e. the
underscore acts here exactly as if spaces exist between words
• The substring is not necessary be exist in the given string and in this case
return 0
The test cases it has to pass are:
hello_world➔ld ➔ 1
hello_world➔l ➔ 3
hello_world_o_w➔o_w ➔ 2
hellohellohellohello➔lo ➔ 4
operating_systems➔ss ➔ 0
defenselessness➔ss ➔ 2
ppppppp➔pp ➔ 3
char* fullString = arguments[1];
char* subString = arguments[2];
int fullLength=strlen(fullString);
int subLength=strlen(subString);
int result = 0;
for(int i=0;i<fullLength;i++){
int check;
for(check=0;check<subLength;check++)
if (fullString[i+check]!=subString[check])
break;
if(check==subLength){
result++;
check=0;
}
}
return result;