Skip to content

Instantly share code, notes, and snippets.

@Mytherin
Created July 5, 2015 21:35
Show Gist options
  • Save Mytherin/7a89816ab58ca460540c to your computer and use it in GitHub Desktop.
Save Mytherin/7a89816ab58ca460540c to your computer and use it in GitHub Desktop.
Count "abcabc" in string
#include <stdio.h>
int countabcabc(char *string);
char match[] = "abcabc";
int main()
{
char string[] = "abcabcabc";
char string2[] = "abcabc22abcababcabcabc";
printf("%d\n", countabcabc(string));
printf("%d\n", countabcabc(string2));
}
int countabcabc(char *string)
{
int state = 0;
int count = 0;
char *pos;
for(pos = string; *pos; pos++)
{
state = state + (match[state] == *pos) - (match[state] != *pos) * state + (match[state] != *pos) * (match[0] == *pos);
count = count + state / 6;
state = state - state / 6 * 3;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment