luc094.c (1360B)
1 /* Read a text file, delete the words 'a', 'the', 'an' and replace each with a blank space. Write to new file. 2 */ 3 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(j) */ 4 5 /* This file is auto-generated by a bot. */ 6 /* This code is not compiled; it is for reference only. */ 7 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <ctype.h> 13 14 void create_article_file(); 15 16 int main() 17 { 18 FILE *fp, *ft; 19 char word[100]; 20 21 create_article_file(); 22 23 fp = fopen("articles.txt", "r"); 24 ft = fopen("clean.txt", "w"); 25 26 if (!fp || !ft) exit(1); 27 28 // Basic word-by-word processing using fscanf 29 // Note: fscanf skips whitespace, so original spacing formatting 30 // might be lost, but it effectively filters words. 31 32 while (fscanf(fp, "%s", word) != EOF) 33 { 34 if (strcasecmp(word, "a") == 0 || 35 strcasecmp(word, "an") == 0 || 36 strcasecmp(word, "the") == 0) 37 { 38 fputc(' ', ft); // Replace with blank 39 } 40 else 41 { 42 fprintf(ft, "%s ", word); 43 } 44 } 45 46 printf("Processed file. Articles removed in 'clean.txt'.\n"); 47 48 fclose(fp); 49 fclose(ft); 50 return 0; 51 } 52 53 void create_article_file() 54 { 55 FILE *f = fopen("articles.txt", "w"); 56 fprintf(f, "The quick brown fox jumps over a lazy dog. It was an honour."); 57 fclose(f); 58 }