c - Count how many times one string appears in another one -


i've been more 3 hours trying solve problem, doesn't work 100%. please me.

problem: create function receives 2 strings (a , b) , show number of times word of string b appears in a, without using function belong library. example:

  • string a: house houuse househousehous
  • string b: house

it needs show word house appears 3x in string a.

#include <stdio.h> #include <stdlib.h>  #include <conio.h>  #include <string.h>  void count_string_b_a(char *a, char *b){    int i,j,k=0,size_a,size_b,equal=0,cont=0;    size_a = strlen(a);     size_b = strlen(b);    j=0;    for(i = 0; < size_b; i++){       for(j = 0; j < size_a; j++){          k=0;         equal=0;         for(k=0; k<size_b; k++){            if(a[j+k] == b[i+k]) equal++;            if(equal==size_b) cont++;         }        }     }        printf("b %s appears %d times in %s",b,cont,a); }  int main(){    int i;    char a[40], b[10];    scanf("%[^\n]s",&a); getchar();    scanf("%[^\n]s",&b);    count_string_b_a(a,b);    getch();  } 

well approach @ problem of calculating how many times word appears in string. custom mystrlen() function imitates strlen function of c. header file need stdio.h

      #include <stdio.h>      int mystrlen(char *s)     {         int length = 0;          while(*s != '\0')         {             length++;             s++;         }         return length;     }      void match(char *haystack, char* needle)     {             int j = 0;              int i,counter=0;              for(i = 0;i < mystrlen(haystack);i++)             {                  if(haystack[i] == needle[j])                 {                     if(j == mystrlen(needle)-1)                     {                         counter++;                         j = 0;                         continue;                     }                        }                 else{                     j = 0;                     continue;                 }                 j++;              }             printf("%d counter shows",counter);     }     int main()     {          char *haystack = "house houuse househousehous";          char *needle = "house";          match(haystack,needle);       }  

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -