Sorting files in C -


i trying write program opens text file, reads file, changes upper case lower case, , counts how many times word has occurred in file , prints results new text file.

my code far follows:

#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <ctype.h> #include <string.h>  int main() {      file *filein;     file *fileout;     char str[255];     char c;     int = 0;      filein = fopen ("input.txt", "r");     fileout = fopen ("output.txt", "w");      if (filein == null || fileout == null)     {         printf("error opening files\n");     }      else     {         while(! feof(filein)) //reading , writing loop         {             fscanf(filein, "%s", str); //reading file               = 0;             c = str[i];             if (isupper(c)) //changing upper case lower case             {                 c =(tolower(c));                 str[i] = putchar(c);             }              printf("%s ", str); //printing output                              fprintf(fileout, "%s\n", str); //printing file         }             fclose(filein);         fclose(fileout);     }     getch(); } 

the input.txt file contains following "the rain in spain falls in plane" don't ask why. after running of program output like: rain in spain falls in plane

i have managed lower case upper case words. having trouble understanding how count occurrences of each word. eg in output want "the 2" meaning 2 had appeared, mean not want more "the" stored in file.

i thinking strcmp , strcpy unsure how use way want.

help appreciated

(sorry if formatting bad)

you may want create hash table words keys , frequencies values.

sketch ideas:

  • recognize words, i.e. alphanumeric string separated white space, try using strtok()
  • for each word
    • search word in hash table based dictionary
      • if found: increment frequency
      • if not found: insert new entry in dictionary (word, 1)

at end, print contents of dictionary, i.e. entries, entry.word , entry.frequency

see question , answer details: quick way implement dictionary in c based on section 6.6 of bible "the c programming language"

update based on op's comment:

hash table efficient table, if not want use it, can still use vanilla tables. here ideas.

typedef struct wordfreq {     char  word[ n ];     int   freq; } wordfreq;  wordfreq wordfreqtable[ t ];  (n maximum length of single word, t maximum number of unique words) 

for searching , inserting, can linear search in table for( int = 0; != t; ++i ) {


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -