summaryrefslogtreecommitdiffstats
path: root/2/5.c
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-11-28 18:52:21 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-11-28 18:52:21 +0800
commitad74a56171901d906cece1462fa573f17423556f (patch)
tree3f621836bdcb6a62edb371215cf9ea3f9c783ff5 /2/5.c
parent9cfaabcb63f06fe563251cc77a8fbcc486b45773 (diff)
downloadk&r-exercises-ad74a56171901d906cece1462fa573f17423556f.tar.gz
2.5
Diffstat (limited to '2/5.c')
-rw-r--r--2/5.c38
1 files changed, 38 insertions, 0 deletions
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 <stdio.h>
+
+#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