Advertisement
michciu

Untitled

Dec 4th, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. lab4zad2
  2. #include <stdio.h>
  3.  
  4. int mystrlen(char* str)
  5. {
  6.     int i=0;
  7.     while(str[i++]!=0);
  8.     return i-1;
  9. }
  10.  
  11. int mystrcmp(char* str1, char* str2)
  12. {
  13.     int flag, i=0;
  14.     do
  15.     {
  16.         flag=(str1[i]==str2[i]);
  17.     }
  18.     while(flag && str1[i]!=0 && str2[i++]!=0);
  19.     return flag;
  20.  
  21. }
  22.  
  23. int isSubstring(char* text, char* substring){
  24.   char *p1, *p2, *p3;
  25.   int i=0,j=0,flag=0;
  26.  
  27.   p1 = text;
  28.   p2 = substring;
  29.  
  30.   for(i = 0; i<mystrlen(text); i++)
  31.   {
  32.     if(*p1 == *p2)
  33.       {
  34.           p3 = p1;
  35.           for(j = 0;j<mystrlen(substring);j++)
  36.           {
  37.             if(*p3 == *p2)
  38.             {
  39.               p3++;p2++;
  40.             }
  41.             else
  42.               break;
  43.           }
  44.           p2 = substring;
  45.           if(j == mystrlen(substring))
  46.           {
  47.             flag = 1;
  48.             printf("Tekst '%s' zawiera ciag znaków '%s'", text, substring);
  49.             return flag;
  50.           }
  51.       }
  52.     p1++;
  53.   }
  54.   if(flag==0)
  55.   {
  56.       printf("nie znaleziono ciagu");
  57.        return -1;
  58.   }
  59.  
  60. }
  61.  
  62. int main()
  63. {
  64.     // Wyznaczanie dlugosci ciagu znakowego
  65.     char tekst1[]="Ala";
  66.     char tekst2[]="Ala ma kota";
  67.  
  68.     printf("Dlugosc ciagu znakowego \"%s\" wynosi %d\n", tekst1, mystrlen(tekst1));
  69.  
  70.     int flag=mystrcmp(tekst1, tekst2);
  71.  
  72.     // Wypisanie wyniku
  73.     if(flag==1)
  74.     {
  75.         printf("Ciagi znakowe \"%s\" oraz \"%s\" sa identyczne.\n", tekst1, tekst2);
  76.     }
  77.     else
  78.     {
  79.         printf("Ciagi znakowe \"%s\" oraz \"%s\" sa rozne.\n", tekst1, tekst2);
  80.     }
  81.    
  82.     isSubstring(tekst2, tekst1);
  83.    
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement