Thread: Pattern within arrays

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    Pattern within arrays

    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();
    }

  2. #2
    Registered User
    Join Date
    Oct 2015
    Posts
    28
    The first thing that sticks out at me is the definition for array1 and array2. I think you want one of the following.

    Code:
    char array1[] = "110100111011";
    char array2[] = "101";
    or

    Code:
    char array1[] = { '1', '1', '0', '1', '0', '0', '1', '1', '1', '0', '1', '1', '\0' };
    char array2[] = { '1', '0', '1', '\0' };
    These two lines are equivalent, either one will do the job. :-)


    So, given these definitions for array1 and array2, we want pattern(array1, array2) to return 2 as the sequence "101" occurs twice in array1. If this is correct, what you'll want to do is learn about strncmp and even try implementing it, because that function will help you implement pattern well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit pattern
    By hell0 in forum C Programming
    Replies: 3
    Last Post: 07-25-2011, 08:38 AM
  2. anti-pattern in GC
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-29-2008, 06:57 AM
  3. C++ Pattern Help
    By Lestat in forum C++ Programming
    Replies: 10
    Last Post: 10-24-2007, 02:05 PM
  4. (pattern *) pat & pattern * pat
    By siubo in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 10:03 PM
  5. pattern
    By Unregistered in forum C Programming
    Replies: 16
    Last Post: 05-04-2002, 07:37 PM

Tags for this Thread