Advertisement
AJTAMjid2000

Chapter 04 exp 01

Jun 26th, 2021
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<string.h>
  3. #define MAXLINE 1000
  4. int getline1(char line[],int max);
  5. int strindex(char source[], char searchfor[]);
  6. char pattern[]  =  "papa";
  7. int main()
  8. {
  9.         char line [MAXLINE];
  10.        int match =0;
  11.         int output[1000]={0};
  12.         int i=0,j;
  13.         while(getline1(line,MAXLINE)>0)
  14.         {
  15.             if(strindex(line,pattern)>=0)
  16.             {
  17.  
  18.                 match=strindex(line,pattern);
  19.                 output[i++]=match;
  20.  
  21.  
  22.  
  23.             }
  24.         }
  25.         for(j=0;j<i;j++)
  26.         {
  27.             printf("The pattern has matched at position  %d\n",output[j]);
  28.         }
  29.  
  30.     return 0;
  31. }
  32. int getline1(char s[],int lim)
  33. {
  34.     int c,i;
  35.     i=0;
  36.     while(--lim >0 && (c=getchar())!=EOF && c!='\n')
  37.     {
  38.         s[i++]=c;
  39.     }
  40.     if(c=='\n')
  41.     {
  42.         s[i++]=c;
  43.  
  44.     }
  45.     s[i]='\0';
  46.     return i;
  47. }
  48. int strindex(char s[],char t[])
  49. {
  50.     int i,j,k;
  51.     for(i=0;s[i]!='\0';i++)
  52.     {
  53.         for(j=i,k=0;t[k]!='\0' && s[j]==t[k];j++,k++)
  54.         {
  55.             ;
  56.         }
  57.         if(k>0 && t[k]=='\0')
  58.         {
  59.             return i;
  60.         }
  61.     }
  62.     return -1;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement