Witam,
mam do napisania program, ktory ma znajdowac i wyswietlac wyrazy z literami 'h' i 'e' obok siebie. Moze ma ktos pomysl, dlaczego nie dziala tak jak powinnio?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define CHUNK 12
char *getWord(FILE *infile);
int selection(char *word, int length);
int main(int argc, char *argv[])
{
char *word, c;
FILE *infile, *outfile;
if(argc < 2)
{
printf("%s","\nMissing arguments.\n");
abort();
}
infile = fopen(argv[1], "r");
if(infile != NULL)
{
outfile = fopen(argv[2], "w");
if(outfile == NULL)
{
printf("It is impossible to open outfile\n");
abort();
}
else
{
while(!feof(infile))
{
word = getWord(infile);
if(word == NULL)
{
free(word);
break;
}
selection(word, strlen(word));
fputs(word, outfile);
if((c = getc(infile)) != EOF)
putc(c, outfile);
free(word);
}
}
}
else
{
printf("It is impossible to open the infile\n");
abort();
}
fclose(infile);
fclose(outfile);
return 0;
}
char *getWord(FILE *infile)
{
int length1, c, cursor;
char *word, *word2;
word = malloc(sizeof(char)*CHUNK);
if(word == NULL) return NULL;
length1 = CHUNK;
cursor = 0;
while(isalpha(c = getc(infile)))
{
word[cursor] = c;
cursor++;
if(cursor >= length1)
{
length1 += CHUNK;
word2 = realloc(word, length1*sizeof(char));
if(word2 == NULL)
{
free(word2);
return NULL;
}
else word2 = word;
}
}
ungetc(c, infile);
word[cursor] = '\0';
return word;
}
int selection(char *word, int length)
{
int i;
for(i = 0; i < length-1; i++)
if(word[i] == 'h' && word[i+1] == 'e')
return 0;
return 1;
}
Generalnie, mam 2 problemy. Albo program przepisuje caly tekst albo wyswietla wyrazy, ktore chce ale bierze tez spacje.