Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lab4zad2
- #include <stdio.h>
- int mystrlen(char* str)
- {
- int i=0;
- while(str[i++]!=0);
- return i-1;
- }
- int mystrcmp(char* str1, char* str2)
- {
- int flag, i=0;
- do
- {
- flag=(str1[i]==str2[i]);
- }
- while(flag && str1[i]!=0 && str2[i++]!=0);
- return flag;
- }
- int isSubstring(char* text, char* substring){
- char *p1, *p2, *p3;
- int i=0,j=0,flag=0;
- p1 = text;
- p2 = substring;
- for(i = 0; i<mystrlen(text); i++)
- {
- if(*p1 == *p2)
- {
- p3 = p1;
- for(j = 0;j<mystrlen(substring);j++)
- {
- if(*p3 == *p2)
- {
- p3++;p2++;
- }
- else
- break;
- }
- p2 = substring;
- if(j == mystrlen(substring))
- {
- flag = 1;
- printf("Tekst '%s' zawiera ciag znaków '%s'", text, substring);
- return flag;
- }
- }
- p1++;
- }
- if(flag==0)
- {
- printf("nie znaleziono ciagu");
- return -1;
- }
- }
- int main()
- {
- // Wyznaczanie dlugosci ciagu znakowego
- char tekst1[]="Ala";
- char tekst2[]="Ala ma kota";
- printf("Dlugosc ciagu znakowego \"%s\" wynosi %d\n", tekst1, mystrlen(tekst1));
- int flag=mystrcmp(tekst1, tekst2);
- // Wypisanie wyniku
- if(flag==1)
- {
- printf("Ciagi znakowe \"%s\" oraz \"%s\" sa identyczne.\n", tekst1, tekst2);
- }
- else
- {
- printf("Ciagi znakowe \"%s\" oraz \"%s\" sa rozne.\n", tekst1, tekst2);
- }
- isSubstring(tekst2, tekst1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement