Advertisement
Guest User

elo

a guest
Mar 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int dlugosc(char*lan);
  4. int sprawdz(char*tekst, char*wzor);
  5. int* sprawdz_w(char* tekst,char*wzor,int* ilosc);
  6.  
  7. int main(){
  8. char slowo[50];
  9. char wzorzec[50];
  10.  
  11. printf("Podaj lancuch: ");
  12. fflush(stdin);
  13. scanf("%49[^\n]s",slowo);
  14. printf("Dlugosc slowa to to: %d\n",dlugosc(slowo));
  15.  
  16. printf("Podaj wzorzec: ");
  17. fflush(stdin);
  18. scanf("%49[^\n]s",wzorzec);
  19. printf("Czy sie zgadza: %d\n",sprawdz(slowo,wzorzec));
  20.  
  21. getch();
  22.  
  23. }
  24.  
  25. int dlugosc(char*lan){
  26.     int i=0;
  27.     while(*(lan+i)!='\0'){
  28.         i++;
  29.     }
  30.     return i;
  31. }
  32.  
  33. int sprawdz(char*tekst, char*wzor){
  34.     int i,dt=dlugosc(tekst),dw=dlugosc(wzor),j,tak=0;
  35.     for(i=0;i<dt-dw+1;i++){
  36.        
  37.         if(*(tekst+i)==*wzor){ tak=1;
  38.             for(j=0;j<dw;j++){
  39.                 if(*(tekst+i+j)!=*(wzor+j)){
  40.                     tak=0; }
  41.             }      
  42.         }
  43.     }
  44.         return tak;
  45. }
  46.  
  47. int* sprawdz_w(char* tekst, char* wzor, int* ilosc)
  48. {
  49.     int* tab=NULL;
  50.     int i=0, j=0, dt=dlugosc(tekst), dw=dlugosc(wzor);
  51.     *ilosc=0;
  52.  
  53.     for(i=0; i<dt-dw+1; i++)
  54.     {
  55.         for(j=0; j<dw; j++)
  56.         {
  57.             if(tekst[i+j]!=wzor[j])
  58.             {
  59.                 break;
  60.             }
  61.         }
  62.         if(j==dw)
  63.         {
  64.             tab=(int*)realloc(tab,(*ilosc+1)*sizeof(int));
  65.             *(tab+*ilosc)=i;
  66.             (*ilosc)++;
  67.         }
  68.     }
  69.  
  70.     return tab;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement