-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_theme.c
70 lines (64 loc) · 1.45 KB
/
generate_theme.c
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
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#define LINE_BUFFER 100
int count_lines( char * filename)
{
char line[LINE_BUFFER];
int lineCount = 0;
FILE * file = fopen(filename, "r");
if (file == NULL) perror ("Error opening file");
else
{
while (fgets(line, LINE_BUFFER, file) != NULL)
{
lineCount++;
}
fclose(file);
}
return lineCount;
}
void get_line(char * filename, int i, char * word) {
char line[LINE_BUFFER];
FILE * file = fopen(filename, "r");
if (file == NULL) perror ("Error opening file");
else
{
int c = 0;
while (fgets(line, LINE_BUFFER, file) != NULL)
{
if (c == i) break;
c++;
}
}
// remove newline
int len = strlen(line);
if( line[len-1] == '\n' )
line[len-1] = 0;
strcpy(word, line);
}
void title(char *str)
{
if (str[0] >= 'a' && str[0] < 'z')
{
str[0]= str[0] - 32;
}
}
main()
{
int verbCount = count_lines("verbs.txt");
int nounCount = count_lines("nouns.txt");
srand(time(NULL));
int i; int vI; int nI;
char verb[LINE_BUFFER];
char noun[LINE_BUFFER];
for (i = 0; i < 100; ++i)
{
vI = rand() % verbCount;
nI = rand() % nounCount;
get_line("verbs.txt", vI, verb);
get_line("nouns.txt", nI, noun);
title(verb); title(noun);
printf("%s the %s\n", verb, noun);
}
}