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

please help me correct my code?count pattern in string

#include<iostream>
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(*(text+a)==*(pattern+a+i))
                            continue;
                        else
                            break;

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

 

}

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 ? 

please

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
...