Advertisement
Guest User

File testing

a guest
Jul 8th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.84 KB | None | 0 0
  1. #include <stdio.h> //for input, output, files
  2. #include <string.h> //for string functions
  3.  
  4. char text[1000]; //text string of size 1000
  5. char words[100][20]; //string array for 100 strings with max of 20-1=19 size
  6. int words_freq[100]; //array for max of 100 word frequency's (they can be less)
  7. int letters_freq[26]; //array with the frequency's of the letters of the alphabet in the text
  8.  
  9. int word_count = 0; //word counter
  10. char *p; //pointer for char functions
  11.  
  12. void read_text_from_file(); //function to read the text from the file
  13. void letter_frequency(); //find letter frequencies
  14. void separate_words(); //find the words from the text, without repeat and with frequency's
  15. void sort_words(); //sorts the words array alphabetically
  16. void print_words_to_file(); //print words to file with first and last letter being caps
  17.  
  18. int main(){
  19.     int i;
  20.     int count;
  21.     char letter; //letter to search for
  22.    
  23.     read_text_from_file();
  24.     printf("Text:\n%s", text); //print text
  25.     letter_frequency();
  26.    
  27.     separate_words();
  28.     sort_words();
  29.     //print words
  30.     printf("\nWords:\n");
  31.     for(i = 0; i < word_count; i++){
  32.         printf("words[%d] = %s (freq = %d)\n", i, words[i], words_freq[i]);
  33.     }
  34.     print_words_to_file();
  35.    
  36.     printf("\nSearch for words starting with: ");
  37.     scanf("%c", &letter);
  38.     fflush(stdin);
  39.     if(letter <= 'Z'){ //if they give big one
  40.         letter = letter + 32;
  41.     }
  42.     count = 0;
  43.     for(i = 0; i < word_count; i++){ //search all words
  44.         if( words[i][0] == letter ){ //check first letter
  45.             printf("%s starts with %c\n", words[i], letter);
  46.             count++;
  47.         }
  48.     }
  49.     if(count == 0){ //if nothing found
  50.         printf("No word starts with %c!\n", letter);
  51.     }else{
  52.         printf("%d words start with %c!\n", count, letter);
  53.     }
  54.    
  55.     printf("\nSearch for words ending with: ");
  56.     scanf("%c", &letter);
  57.     fflush(stdin);
  58.     if(letter <= 'Z'){ //if they give big one
  59.         letter = letter + 32;
  60.     }
  61.     count = 0;
  62.     for(i = 0; i < word_count; i++){ //search all words
  63.         if( words[i][strlen(words[i])-1] == letter ){ //check last letter
  64.             printf("%s starts with %c\n", words[i], letter);
  65.             count++;
  66.         }
  67.     }
  68.     if(count == 0){ //if nothing found
  69.         printf("No word ends with %c!\n", letter);
  70.     }else{
  71.         printf("%d words end with %c!\n", count, letter);
  72.     }
  73.    
  74.    
  75.    
  76.    
  77. }
  78.  
  79. void read_text_from_file(){
  80.     char buf[80]; //buffer for reading line by line
  81.     FILE *fp; //file pointer
  82.    
  83.     fp = fopen("text.txt", "r"); //open the text for reading
  84.     if(fp == NULL){ //error checking
  85.         printf("File not found!\n");
  86.     }
  87.    
  88.     fgets(buf, 80, fp); //read from the file
  89.     strcpy(text, buf); //set the text to the first buffer
  90.     while( fgets(buf, 100, fp) != NULL){ //read until end of file
  91.         strcat(text, buf); //add buffer at the end of the string, concatenating them
  92.     }  
  93.     fclose(fp); //close the file   
  94. }
  95.  
  96. void letter_frequency(){
  97.     int i, j;
  98.     char letter = 'a';
  99.    
  100.     for(i = 0; i < 26; i++){ //for every letter in the alphabet
  101.         letters_freq[i] = 0;
  102.         for(j = 0; j < strlen(text); j++){
  103.             if(text[j] == letter || text[j] == letter - 32){ //if it is the small or cap letter
  104.                 letters_freq[i]++;
  105.             }
  106.         }  
  107.         letter++; //go to next letter
  108.     }
  109.     letter = 'a';
  110.     printf("\nFrequency of Letters:\n");
  111.     for(i = 0; i < 26; i++){
  112.         printf("%c: %d\n", letter, letters_freq[i]);
  113.         letter++;
  114.     }
  115. }
  116.  
  117. void separate_words(){
  118.     int i;
  119.     int number, min_length, same; //flags for words
  120.    
  121.     p = strtok(text," \"\n\t\r,.-;!"); //p will point at the next address that is not one of the tokens (second parameter)
  122.     while (p != NULL){ //while not at the end of the text
  123.         number = 0; //no number in "word"
  124.         min_length = 1; //length must be at least 2 to be a word
  125.        
  126.         if(strlen(p) > 1); //if enough do nothing
  127.         else min_length = 0; //too small length
  128.        
  129.         for(i = 0; i < strlen(p); i++){ //search whole "word" for numbers
  130.             if(p[i] >= '0' && p[i] <= '9'){ //numbers are in character form and because of ASCII one after another
  131.                 number = 1; //there is a number inside
  132.             }
  133.         }
  134.        
  135.         //make all capital letters small letters
  136.         if( p[0] <= 'Z'){ //in ASCII all characters are in alphabetic order
  137.                 p[0] = p[0] + 32; //small ones are 32 "higher" then capital ones
  138.         }
  139.        
  140.         if(number == 0 && min_length == 1){  //if it is doesn't have numbers and is at least of length two
  141.             same = 0; //words is not inserted yet
  142.             //search for the word in all the inserted words
  143.             for(i = 0; i <word_count; i++){
  144.                 if( strcmp(p, words[i]) == 0 ){ //if p and words[i] are equal it will return 0
  145.                     same = 1; //word already inserted
  146.                     words_freq[i]++; //increment the frequency of the word
  147.                     break; //break the for loop, cause you can't find it another time
  148.                 }
  149.             }
  150.             if(same == 0){ //if it was not inserted
  151.                 strcpy(words[word_count], p); //insert word into array
  152.                 words_freq[word_count] = 1; //set frequency to 1
  153.                 word_count++; //update word counter
  154.             }          
  155.         }
  156.         p = strtok(NULL," \"\n\t\r,.-;!"); //continue from where you stopped at the text searching for words
  157.        
  158.     }
  159. }
  160.  
  161. void sort_words(){
  162.     int i, j;
  163.     char word[20]; //temporary word for swapping
  164.    
  165.     for(i = 0; i < word_count; i++){
  166.         for(j = i; j < word_count; j++){
  167.             if( strcmp(words[i], words[j]) > 0 ){ //when it returns > 0 it means first one is higher alphabetically
  168.                 //swap using temporary variable
  169.                 strcpy(word, words[i]);
  170.                 strcpy(words[i], words[j]);
  171.                 strcpy(words[j], word);
  172.             }
  173.         }
  174.     }
  175. }
  176.  
  177. void print_words_to_file(){
  178.     FILE *fp; //file pointer
  179.     int i;
  180.     char word[20]; //temporary word for editing
  181.    
  182.     fp = fopen("words.txt", "w"); //open file for writing
  183.    
  184.     for(i = 0; i < word_count; i++){
  185.         strcpy(word, words[i]);
  186.         if(words[i][0] >= 'a') word[0] = word[0] - 32; //if first letter small then make it cap with -32
  187.         if(words[i][ strlen(words[i])-1 ] >= 'a') word[strlen(words[i])-1] = word[strlen(words[i])-1] - 32; //if last one is small do the same
  188.         fprintf(fp, "%s\n", word); //write word to file
  189.     }
  190.    
  191.     fclose(fp); //close file
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement