.
This commit is contained in:
37
bac2/os/chap0/ex7.c
Normal file
37
bac2/os/chap0/ex7.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
unsigned int count_word_occurence(char string[], char target[]);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char test[] = "I am a test and im diggin a test... diggy diggy test...";
|
||||
printf("%s -> %d\n", test, count_word_occurence(test, "test"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int count_word_occurence(char string[], char target[]){
|
||||
int sizeoftarget = 0;
|
||||
while(target[sizeoftarget] != '\0'){
|
||||
sizeoftarget++;
|
||||
}
|
||||
|
||||
int i = 0, ret = 0;
|
||||
while(string[i] != '\0'){
|
||||
if(string[i] == target[0]){
|
||||
int check = 0;
|
||||
for (int j = 0; j < sizeoftarget; ++j) {
|
||||
if(target[j] == string[i+j]){
|
||||
continue;
|
||||
}
|
||||
check = 1;
|
||||
}
|
||||
if(!check){
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return ret;
|
||||
}
|
Reference in New Issue
Block a user