Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

please tell me the mistakes in this program(find no of sub string)

0 votes
asked Feb 28, 2020 by anonymous
#include <bits/stdc++.h>

using namespace std;
int FindSubString(char *a,char *b,int i);
int main()
{
    char a[100],b[100];
    fgets(a,100,stdin);
    cin>>b;
    int count=0,subcount;
    for(int i=0;i<sizeof(a);i++){
        subcount=FindSubString(a,b,i);
        if(subcount==sizeof(b))count++;
    }
    cout<<"total no of sub string : "<<count<<endl;
    return 0;
}
int FindSubString(char *a,char *b,int i){
    int k=0,count=0;
    for(int j=i;a[j]!='\0';j++){
        if(b[k]==a[i]){
            count++;
            k++;
        }
        else break;
    }
    return count;
}

1 Answer

0 votes
answered Mar 2, 2020 by anonymous

The problem is likely in the line:

if(subcount==sizeof(b))count++;

This line assume the lenght of b is 100.  You can use strlen instead

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...