Počítanie slov vo vete s výstupom do súboru
Autor: Gabor Tomas
Kód:
/*! zadanie:
Mam spravit program ktory ma spocitat slova vo vete, slova musia byt oddelene
jednou medzerou a veta musi koncit bodkou, ma byt pouzita funkcia a zapis do
suboru. */
#include <stdio.h> // scanf, printf, fwrite, fclose, fopen, sprintf
#include <stdlib.h> // system
#include <string.h> // strcmp
int main()
{
char word[100];
int count = 0;
int len = 0;
printf("hallo, enter sentence (. for end): ");
while (scanf("%s", word) && strcmp(word, "."))
{
int len = strlen(word);
count++;
if (word[len - 1] == '.')
break;
}
FILE * fout;
if (!(fout = fopen("out.txt", "w")))
return 1;
sprintf(word, "you entered %i words", count);
len = strlen(word);
fwrite(word, sizeof(char) * len, 1, fout);
fclose(fout);
printf("see <application_dir>\\out.txt\n");
system("pause");
return 0;
}