Advance pattern recognition.

Why my code doesn't work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int rgrep_matches(char *line, char *pattern) {
    
   
     /*int num = check_slash(line);
     int start = 0;
     char slice_before[num + 1];
     char slice_after[num + 1];
     memcpy(slice_before, line + start, (num -1)*sizeof(char)); //after before 
     memcpy(slice_after, line + num, (num)*sizeof(char)); //after slash
     slice_before[num] = 0;
     slice_after[num] = 0;*/
     //for (int i = check_slash(line); i <= length(line); i++){
     //printf("%c",line[i]);
	int i = 0;
	int j = 0;
	int counter = 0;
	int slash_counter = 0;
	while (1){
	if (line[j] == pattern[i]){
	  i++;
	  j++;
	  if(counter == 0){counter++;}}// end of both if loop
	else{
	if (pattern[i] == '.'){i++;}j++;}

	if (pattern[i] == '\0')
	{printf("you did it!!");
	return 1;}

	if (line[j] == '\0'){
	printf("you failed!!");
	return 0;}

	if ((counter ==  1) & (line[j] != pattern[i]) & (pattern[i] != '.')){
	printf("you failed counter > 1");
	return 0;}
	}

    return 0;
}


pattern: hello
line: hello
passed!

pattern: hel..lo (dot can mean anything char)
line: hel34lo
passed!

pattern: hello\hello
line: hello\hello
failed! why?
Last edited on
Topic archived. No new replies allowed.