Advertisement
Aritra15

word_repitition

Oct 9th, 2022
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     char str[]="ab ab abc";
  5.     char word[]="ab";
  6.  
  7.     int lens=0,lenw=0,i,j,count=0,c=0;
  8.  
  9.     //finding length of string and word
  10.     while(str[lens] !='\0')
  11.     {
  12.         lens++;
  13.     }
  14.  
  15.     while(word[lenw] !='\0')
  16.     {
  17.         lenw++;
  18.     }
  19.  
  20.  
  21.     for(i=0;i<lens;i++)
  22.     {
  23.         if(str[i]==word[0])
  24.         {
  25.            for(j=0;j<lenw;j++)
  26.            {
  27.                if(str[i+j]==word[j])
  28.                {
  29.                    count++;
  30.                }
  31.            }
  32.            if(count==lenw)
  33.            {
  34.                //here c stores the number of times the word is repeated in the string.
  35.                c++;
  36.                count=0;
  37.            }
  38.         }
  39.     }
  40.  
  41.     printf("%d",c);
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement