Code: Alles auswählen
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 1000
void skip_whitespace(FILE *f)
{
int c;
do {
c = fgetc(f);
} while (isspace(c) && c != EOF);
if (c != EOF) ungetc(c, f);
}
void read_sentence(FILE *f, char *buffer)
{
unsigned int i;
int c;
for (i = 0; i < BUFFER_SIZE - 1; ++i) {
c = fgetc(f);
if (c == EOF) break;
*buffer++ = c;
if (c == '.') break;
}
*buffer = '\0';
assert(c == EOF || c == '.');
}
bool contains_word(char *string, char *word)
{
char *tmp;
unsigned int start_index, end_index;
tmp = strstr(string, word);
if (!tmp) return false;
start_index = tmp - string;
end_index = start_index + strlen(word);
return (start_index == 0
|| !isalnum(string[start_index - 1]))
&& (end_index == strlen(string - 1)
|| !isalnum(string[end_index]));
}
int main(void)
{
FILE *f;
char sentence[BUFFER_SIZE];
char *filename = "winter's tale";
f = fopen(filename, "r");
if (!f) {
printf("could not open '%s'\n", filename);
return 1;
}
while (!feof(f)) {
skip_whitespace(f);
read_sentence(f, sentence);
if (contains_word(sentence, "Luzifer")) {
puts(sentence);
}
}
fclose(f);
return 0;
}
[codebox=text file=Unbenannt.txt]LOAD"$",8
SEARCHING FOR $
LOADING
READY.
LIST
0 "TEST-DISK " 00 2A
36 "123456789012 PIC" PRG
21 "TEST" PRG
26 "WINTER'S TALE" SEQ
581 BLOCKS FREE.
READY.
LOAD"TEST",8,1
SEARCHING FOR TEST
LOADING $0801 $1BEE
READY.
RUN
Dort trifft er auf seinen Boss Luzifer,
den Richter.
Pearly ersucht Luzifer um die Genehmigun
g, ausnahmsweise Zugang zu einer Adresse
ausserhalb der Stadt zu bekommen: zu Be
verlys Haus am See; der hohe Richter Luz
ifer lehnt Pearlys Antrag ab.
Pearly hat mit Luzifer einen Deal ausgeh
andelt: Wenn er bereit sei, sich sterbli
ch zu machen, duerfe er Peter auch ausse
rhalb der Stadt verfolgen.
ready.[/code]