From ad74a56171901d906cece1462fa573f17423556f Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 28 Nov 2021 18:52:21 +0800 Subject: 2.5 --- 2/5.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2/5.c (limited to '2/5.c') diff --git a/2/5.c b/2/5.c new file mode 100644 index 0000000..0c21eac --- /dev/null +++ b/2/5.c @@ -0,0 +1,38 @@ +#include + +#define MAXLEN 1000 + +int any(char s1[], char s2[]); + +/* finds the location of first occurrence of any character */ +int main(int argc, char *argv[]) { + int i, c; + char s1[MAXLEN], s2[MAXLEN]; + + printf("Enter first string\n"); + for (i = 0; i < MAXLEN - 1 && (c = getchar()) != EOF && c != '\n'; i++) + s1[i] = c; + s1[i] = 0; + + printf("Enter second string\n"); + for (i = 0; i < MAXLEN - 1 && (c = getchar()) != EOF && c != '\n'; i++) + s2[i] = c; + s2[i] = 0; + + printf("Index: %d\n", any(s1, s2)); + + return 0; +} + +int any(char s1[], char s2[]) { + int i, j; + + for (i = 0; s1[i] != 0; i++) { + for (j = 0; s2[j] != 0; j++) { + if (s1[i] == s2[j]) + return i; + } + } + + return -1; +} \ No newline at end of file -- cgit v1.2.3