Reputation: 71
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;
Upvotes: 0
Views: 375
Reputation: 11377
Here is a solution using the function strstr from the standard library:
#include <assert.h>
#include <stdio.h>
#include <string.h>
int MatchCount(const char pattern[], const char target[])
{
int n, patternLength;
const char *p;
patternLength = strlen(pattern);
n = 0;
if (patternLength > 0) {
p = strstr(target, pattern);
while (p != NULL) {
n++;
p = strstr(p + patternLength, pattern);
}
}
return n;
}
int main(void)
{
assert(MatchCount("", "") == 0);
assert(MatchCount("foo", "") == 0);
assert(MatchCount("", "foo") == 0);
assert(MatchCount("foo", "bar") == 0);
assert(MatchCount("foo", "foo") == 1);
assert(MatchCount("foo", "foo bar") == 1);
assert(MatchCount("foo", "bar foo") == 1);
assert(MatchCount("foo", "foo bar foo") == 2);
assert(MatchCount("foo", "bar foo foo") == 2);
assert(MatchCount("pp", "ppp") == 1);
assert(MatchCount("pp", "pppp") == 2);
return 0;
}
Upvotes: 0
Reputation: 1738
This is not a great way or very efficient method, but its simple to understand how it works.
works with the following combinations:
//char shells[] = "ppppppp";
//char repeat[] = "pp";
//char shells[] = "defenselessness";
//char repeat[] = "ss";
//char shells[] = "ppppppp";
//char repeat[] = "ppp";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char shells[] = "she sells sea shells, but sea shells she sells are not real sea shells";
char repeat[] = "shells";
int noccur = 0, i = 0;
int len = strlen(repeat);
while(shells[i])
{
if ( //length should be more than repeat length, if not we can stop
( strlen(shells+i) >= len ) &&
// iterating only one char at a time
(!strncmp(shells+i, repeat, len))
)
{
//match occurs we can actually increase the pos(i) with length of repeat word
i = i+len-1;
noccur++;
}
i++;
}
printf("%s occured %d times\n", repeat, noccur);
return 0;
}
Upvotes: 1