aboutsummaryrefslogtreecommitdiff
path: root/4.1.c
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-06 13:24:34 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-06 13:24:34 +0530
commit31a036f54543af5c6d9cabea58b021620e888035 (patch)
treee2b3f5237e79a7d729ae10b51a7eb7e02c689713 /4.1.c
parentf690af3d466efd4d1ff45f0e79a0288be4f4c915 (diff)
added 4.1 and 4.2
Diffstat (limited to '4.1.c')
-rw-r--r--4.1.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/4.1.c b/4.1.c
new file mode 100644
index 0000000..e848d0c
--- /dev/null
+++ b/4.1.c
@@ -0,0 +1,61 @@
+#include <stdio.h>
+
+#define MAXLEN 1000
+
+int get_line(char line[], const int max);
+int strindex(char source[], char pattern[]);
+
+int
+main(void)
+{
+ int found, rightmost;
+ char line[MAXLEN];
+
+ char pattern[] = "ould";
+ found = 0;
+
+ while (get_line(line, MAXLEN) > 0)
+ if ((rightmost = strindex(line, pattern)) >= 0) {
+ printf("%s >> match at char %d\n", line, rightmost+1);
+ found++;
+ }
+
+ printf("\nSneedGrep found %d match%s\n", found, (found > 0) ? "es" : "");
+
+ return 0;
+}
+
+int
+get_line(char line[], const int max)
+{
+ int i;
+ char input;
+
+ for (i = 0; (input = getchar()) != EOF && input != '\n' && i < max-2; i++)
+ line[i] = input;
+
+ if (input == '\n' && i < max-2)
+ line[i++] = '\n';
+
+ line[i] = '\0';
+
+ return i;
+}
+
+int
+strindex(char source[], char pattern[])
+{
+ int i, j, k, rightmost;
+
+ rightmost = -1;
+
+ for (i = 0; source[i] != '\0'; i++) {
+ for (j = i, k = 0; pattern[k] != '\0' && source[j] == pattern[k]; j++, k++)
+ ;
+
+ if (pattern[k] == '\0')
+ rightmost = i;
+ }
+
+ return rightmost;
+}