Advertisement
Guest User

C algorytmy wyszukiwania fraz w tekscie

a guest
Mar 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int dlugosc(char*tekst);
  5. int sprawdz(char*tekst, char*wzor);
  6. int* sprawdz_w(char*tekst, char*wzor, int*ilosc);
  7.  
  8.  
  9. int main()
  10. {
  11.  
  12. char tekst[100];
  13. char wzor[100];
  14. int ilosc;
  15. int dlugosct;
  16. int dlugoscw;
  17. int repeat = 1;
  18. int wynik;
  19. int *tab = NULL;
  20.  
  21.  
  22.  
  23. printf("Witaj\n Podaj tekst do sprawdzenia:\n");
  24. fflush(stdin);
  25. scanf("%99[^\n]s", tekst);
  26.  
  27. printf("\n Podaj wzor do wyszukania:\n");
  28. fflush(stdin);
  29. scanf("%99[^\n]s", wzor);
  30.  
  31. dlugosct = dlugosc(tekst);
  32. dlugoscw = dlugosc(wzor);
  33.  
  34. wynik = sprawdz(tekst,wzor);
  35. if(wynik<0) printf("W podanym tekscie niestety nie ma wpisanej frazy");
  36. else tab = sprawdz_w(tekst, wzor, &ilosc);
  37. printf("\n%d", ilosc);
  38.  
  39.  
  40.  
  41.  
  42. getch();
  43. }
  44.  
  45. int dlugosc(char*tekst)
  46. {
  47.     int i =0;
  48.     while(tekst[i]!='\0')
  49.     {
  50.         i++;
  51.     };
  52.     return i;
  53. }
  54.  
  55. int sprawdz(char*tekst,char*wzor)
  56. {
  57.     int i,j,JestWzorzec=0,dlugoscTekst,dlugoscWzor;
  58.     dlugoscTekst=dlugosc(tekst);
  59.     dlugoscWzor=dlugosc(wzor);
  60.  
  61.   for (i=0;i<dlugoscTekst -dlugoscWzor +1;i++)
  62.   {
  63.         if(*(tekst+i)==*(wzor))
  64.         {
  65.             for (j=1;j<dlugoscWzor;j++)
  66.             {
  67.                if (*(tekst+i+j)!=*(wzor+j))
  68.                {
  69.                     JestWzorzec=0;                  
  70.                     break;
  71.                }
  72.                
  73.                 JestWzorzec=1;
  74.  
  75.                 }
  76.                 if(JestWzorzec==1)
  77.                 {                    
  78.                 return i;
  79.                 }
  80.         }
  81.   };
  82.   return -1;
  83. }
  84.  
  85.  
  86. int* sprawdz_w(char*tekst, char*wzor, int*ilosc)
  87. {
  88.     int i,j,JestWzorzec=0,dlugoscTekst,dlugoscWzor, ile;
  89.     int *tab = NULL;
  90.     dlugoscTekst=dlugosc(tekst);
  91.     dlugoscWzor=dlugosc(wzor);
  92.     printf("\nIndeksy kolejnych wzorcow:  ");
  93.     ile=0;
  94.  
  95.   for (i=0;i<dlugoscTekst -dlugoscWzor +1;i++)
  96.   {
  97.  
  98.         if(*(tekst+i)==*(wzor))
  99.         {
  100.             for (j=1;j<dlugoscWzor;j++)
  101.             {
  102.                if (*(tekst+i+j)!=*(wzor+j))
  103.                {
  104.                     JestWzorzec=0;                  
  105.                     break;
  106.                }
  107.                
  108.                 JestWzorzec=1;
  109.  
  110.                 }
  111.                 if(JestWzorzec==1)
  112.                 {                    
  113.                 ile=ile+1;
  114.                 printf("%d,  ", i);
  115.                 tab = (int*) realloc(tab, sizeof(int)*ile);
  116.                 tab[ile-1] = i;
  117.                 }
  118.         }
  119.   };
  120.  
  121.   *ilosc = ile;
  122.   return tab;
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement