Created
July 5, 2015 21:35
-
-
Save Mytherin/7a89816ab58ca460540c to your computer and use it in GitHub Desktop.
Count "abcabc" in string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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