GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

please help me correct my code?count pattern in string

using namespace std;

char* text= new char;
char* pattern= new char;

int countPattern(char *pattern){
                int count=0,a;
                for(int i=0;i<(sizeof text- sizeof pattern);i++){
                    for (a=0;a<sizeof pattern;a++){

                    if(a==sizeof pattern){
                return count;}
    cout<<"enter the text\n";
    cout<<"enter pattern\n";
    int count=countPattern(pattern);



asked Aug 22, 2016 by vans

the main method i think should be:

int main(){
return 0;


Then if your counting patterns what's the comparison supposed to achieve. Again the text variable is never passed to the countPattern function. I think c++  char  data type takes on char not sure on that.

2 Answers

Can you please post the question ? or any example to elaborate what you are trying to do ? 


answered Aug 22, 2016 by Mazhar MIK
The question is to count a pattern say 'abc' in a given continuous string say 'acbcabcdacdcccabcdabacabc' So here my function should return 3
answered Aug 23, 2016 by anonymous