aboutsummaryrefslogtreecommitdiff
path: root/5.6.3.c
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-18 16:13:44 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-18 16:13:44 +0530
commitff4846af7dea497d75af09acfa6c2c8ca48fd67e (patch)
tree974ad1b5f260c398405c04578b90155480250aa5 /5.6.3.c
parentc55c5080200f0f31ce05485af7cce4cf4204ac40 (diff)
5.6
Diffstat (limited to '5.6.3.c')
-rw-r--r--5.6.3.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/5.6.3.c b/5.6.3.c
new file mode 100644
index 0000000..a186c1f
--- /dev/null
+++ b/5.6.3.c
@@ -0,0 +1,62 @@
+#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, rightmost;
+ char *source_og = source;
+
+ rightmost = -1;
+
+ for (; *source; source++) {
+ for (i = 0; *(pattern +i) && *(source+i) == *(pattern+i); i++)
+ ;
+
+ if (!*(pattern+i))
+ rightmost = source - source_og;
+ }
+
+ return rightmost;
+}