Advertisement
levartolona

C_PRG_LANG_EX_2.5

Jan 26th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. // Returns position of first met of any symbol from s2 in s1
  7. // Returns -1 if there`s no symbols from s2 in s1
  8. int any(char *s1, char *s2)
  9. {
  10.     for (int i = 0; s1[i] != '\0'; i++)
  11.         for (int j = 0; s2[j] != '\0'; j++)
  12.             if (s1[i] == s2[j])
  13.                 return i;
  14.  
  15.     return -1;
  16. }
  17.  
  18. int main(void)
  19. {
  20.     char *str1 = malloc(sizeof(char) * 256);
  21.     char *str2 = malloc(sizeof(char) * 256);
  22.     strcpy(str1, "Mother washed the window");
  23.     strcpy(str2, "tad");
  24.     printf("%s\n%s\n%i\n", str1, str2, any(str1, str2));
  25.  
  26.     strcpy(str2, "ad");
  27.     printf("%s\n%s\n%i\n", str1, str2, any(str1, str2));
  28.  
  29.     strcpy(str2, "d");
  30.     printf("%s\n%s\n%i\n", str1, str2, any(str1, str2));
  31.     free(str1);
  32.     free(str2);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement