Hello Everybody,
I am tasked to write a program that counts the number of times a character sequence occurs in another character sequence. The 'pattern' functions should return the the number of times the first sequence occurs in the second sequence without repeating. As this is a programming exercise, I'm not allowed to use brackets to dereference the pointers and must only use pointer arithmetic.
I decided to use if statements to see if a sequence occurs in the array. I'm having a difficult time seeing where my issues are. I print out pValue1 to see where the pointer is located in the array. Any help would be appreciated.
Code:
#include <stdio.h>
#include <string.h>
int pattern(char *ptr1, char *ptr2)
{
char *pValue1 = ptr1;
char *pValue2 = ptr2;
int count = 0;
int occurrence = 0;
while (*pValue1 != '\0')
{
if (*pValue1 == *pValue2)
{
if (*pValue1++ == *pValue2++)
{
if (*pValue1 + 2 == *pValue2 + 2)
{
printf("Occurence happens at Location: %d", count);
occurrence++;
}
}
}
count++;
pValue1 = pValue1 + count;
printf("%d", pValue1);
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n", occurrence);
}
void main()
{
char array1[] = "1','1','0','1','0','0','1','1','1','0','1','1','\0'";
char array2[] = "'1','0', '1', '\0'";
pattern(array1, array2);
getchar();
getchar();
}