summaryrefslogtreecommitdiff
path: root/algorithms/bigrams.c
blob: ca40f49f531f9f993a995d25d918fb7bb539ae14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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;
}