summaryrefslogtreecommitdiff
path: root/algorithms/bigrams.c
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
committerPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
commitcf3a29feb5887344b6633ead1b4b6d5657a15a4b (patch)
treef1149508f7305a48dba0226699dfafdd68d81969 /algorithms/bigrams.c
parent5ddc763ab9953eebdaf78af4eb72288d7955b310 (diff)
old stuff: algorithms
Diffstat (limited to 'algorithms/bigrams.c')
-rw-r--r--algorithms/bigrams.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/algorithms/bigrams.c b/algorithms/bigrams.c
new file mode 100644
index 0000000..ca40f49
--- /dev/null
+++ b/algorithms/bigrams.c
@@ -0,0 +1,116 @@
+#include <stdio.h>
+
+#define OUTPUT_WIDTH 72
+
+#define LARGE_ARRSZ 500
+#define SMALL_ARRSZ 5
+
+
+struct bigram {
+ char trail, lead;
+};
+
+int
+get_bigrams(char *text, struct bigram *bigrams, int max_bigrams)
+{
+ char *trail=text, *lead=text+1;
+ int i=0;
+
+ while(*trail && *lead && (i<max_bigrams)) {
+ if (LARGE_ARRSZ<i) {
+ return -1;
+ }
+ bigrams[i].trail = *trail++;
+ bigrams[i].lead = *lead++;
+ i++;
+ }
+
+ return i;
+}
+
+void
+print_bigrams(struct bigram *bigrams, int num_bigrams, int line_width)
+{
+ int i, retval=0;
+
+ for(i = 0; i < num_bigrams; i += 1) {
+ retval += (printf("%c%c/", bigrams->trail, bigrams->lead));
+ if (retval > line_width) {
+ printf("\n");
+ retval=0;
+ }
+ *bigrams++;
+ }
+ printf("\n");
+}
+
+void
+test1(char *my_text)
+{
+ struct bigram largeArr[LARGE_ARRSZ];
+ int num_bigrams;
+
+ if ((num_bigrams = get_bigrams(my_text, largeArr, LARGE_ARRSZ))<LARGE_ARRSZ) {
+ printf("%d bigrams:\n", num_bigrams);
+ print_bigrams(largeArr, num_bigrams, OUTPUT_WIDTH);
+ }
+}
+
+void
+test2(char *my_text)
+{
+ struct bigram smallArr[SMALL_ARRSZ+1];
+ int num_bigrams;
+
+ smallArr[SMALL_ARRSZ].lead = '@'; /* sentinel for overwrites */
+ if ((num_bigrams = get_bigrams(my_text, smallArr, SMALL_ARRSZ))<SMALL_ARRSZ) {
+ printf("wrong bigram count!\n");
+ }
+ if (smallArr[SMALL_ARRSZ].lead!='@') {
+ printf("buffer overflow :-(\n");
+ }
+}
+
+int
+do_something(char *text, struct bigram *bigrams, int max)
+{
+ char *trail=text, *lead=text+1;
+ int i=0;
+
+ while(*trail && *lead && (i < max)) {
+ bigrams[i].trail = *trail++;
+ bigrams[i].lead = *lead++;
+ i++;
+ }
+
+ for(unsigned int j = 0; j < max; j += 1) {
+ printf("%c%c/\n", bigrams[j].trail, bigrams[j].lead);
+ }
+
+ printf("%d\n", i);
+
+ return i;
+}
+
+int main(void)
+{
+ struct bigram b = {'a', 'b'};
+ struct bigram b1 = {'b', 'c'};
+ struct bigram bigrams[3];
+ *bigrams = b;
+ *(bigrams+1) = b1;
+ for(unsigned int i = 0; i < 2; i += 1) {
+ printf("%c%c\n", bigrams[i].trail, bigrams[i].lead);
+ }
+
+ struct bigram bigrams1[3];
+ char *text = "abcd";
+ int max=3;
+ do_something(text, bigrams1, max);
+
+ char *my_text = "Ein Gespenst geht um in Europa";
+ test1(my_text);
+ test2(my_text);
+
+ return 0;
+}