Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> //for input, output, files
- #include <string.h> //for string functions
- char text[1000]; //text string of size 1000
- char words[100][20]; //string array for 100 strings with max of 20-1=19 size
- int words_freq[100]; //array for max of 100 word frequency's (they can be less)
- int letters_freq[26]; //array with the frequency's of the letters of the alphabet in the text
- int word_count = 0; //word counter
- char *p; //pointer for char functions
- void read_text_from_file(); //function to read the text from the file
- void letter_frequency(); //find letter frequencies
- void separate_words(); //find the words from the text, without repeat and with frequency's
- void sort_words(); //sorts the words array alphabetically
- void print_words_to_file(); //print words to file with first and last letter being caps
- int main(){
- int i;
- int count;
- char letter; //letter to search for
- read_text_from_file();
- printf("Text:\n%s", text); //print text
- letter_frequency();
- separate_words();
- sort_words();
- //print words
- printf("\nWords:\n");
- for(i = 0; i < word_count; i++){
- printf("words[%d] = %s (freq = %d)\n", i, words[i], words_freq[i]);
- }
- print_words_to_file();
- printf("\nSearch for words starting with: ");
- scanf("%c", &letter);
- fflush(stdin);
- if(letter <= 'Z'){ //if they give big one
- letter = letter + 32;
- }
- count = 0;
- for(i = 0; i < word_count; i++){ //search all words
- if( words[i][0] == letter ){ //check first letter
- printf("%s starts with %c\n", words[i], letter);
- count++;
- }
- }
- if(count == 0){ //if nothing found
- printf("No word starts with %c!\n", letter);
- }else{
- printf("%d words start with %c!\n", count, letter);
- }
- printf("\nSearch for words ending with: ");
- scanf("%c", &letter);
- fflush(stdin);
- if(letter <= 'Z'){ //if they give big one
- letter = letter + 32;
- }
- count = 0;
- for(i = 0; i < word_count; i++){ //search all words
- if( words[i][strlen(words[i])-1] == letter ){ //check last letter
- printf("%s starts with %c\n", words[i], letter);
- count++;
- }
- }
- if(count == 0){ //if nothing found
- printf("No word ends with %c!\n", letter);
- }else{
- printf("%d words end with %c!\n", count, letter);
- }
- }
- void read_text_from_file(){
- char buf[80]; //buffer for reading line by line
- FILE *fp; //file pointer
- fp = fopen("text.txt", "r"); //open the text for reading
- if(fp == NULL){ //error checking
- printf("File not found!\n");
- }
- fgets(buf, 80, fp); //read from the file
- strcpy(text, buf); //set the text to the first buffer
- while( fgets(buf, 100, fp) != NULL){ //read until end of file
- strcat(text, buf); //add buffer at the end of the string, concatenating them
- }
- fclose(fp); //close the file
- }
- void letter_frequency(){
- int i, j;
- char letter = 'a';
- for(i = 0; i < 26; i++){ //for every letter in the alphabet
- letters_freq[i] = 0;
- for(j = 0; j < strlen(text); j++){
- if(text[j] == letter || text[j] == letter - 32){ //if it is the small or cap letter
- letters_freq[i]++;
- }
- }
- letter++; //go to next letter
- }
- letter = 'a';
- printf("\nFrequency of Letters:\n");
- for(i = 0; i < 26; i++){
- printf("%c: %d\n", letter, letters_freq[i]);
- letter++;
- }
- }
- void separate_words(){
- int i;
- int number, min_length, same; //flags for words
- p = strtok(text," \"\n\t\r,.-;!"); //p will point at the next address that is not one of the tokens (second parameter)
- while (p != NULL){ //while not at the end of the text
- number = 0; //no number in "word"
- min_length = 1; //length must be at least 2 to be a word
- if(strlen(p) > 1); //if enough do nothing
- else min_length = 0; //too small length
- for(i = 0; i < strlen(p); i++){ //search whole "word" for numbers
- if(p[i] >= '0' && p[i] <= '9'){ //numbers are in character form and because of ASCII one after another
- number = 1; //there is a number inside
- }
- }
- //make all capital letters small letters
- if( p[0] <= 'Z'){ //in ASCII all characters are in alphabetic order
- p[0] = p[0] + 32; //small ones are 32 "higher" then capital ones
- }
- if(number == 0 && min_length == 1){ //if it is doesn't have numbers and is at least of length two
- same = 0; //words is not inserted yet
- //search for the word in all the inserted words
- for(i = 0; i <word_count; i++){
- if( strcmp(p, words[i]) == 0 ){ //if p and words[i] are equal it will return 0
- same = 1; //word already inserted
- words_freq[i]++; //increment the frequency of the word
- break; //break the for loop, cause you can't find it another time
- }
- }
- if(same == 0){ //if it was not inserted
- strcpy(words[word_count], p); //insert word into array
- words_freq[word_count] = 1; //set frequency to 1
- word_count++; //update word counter
- }
- }
- p = strtok(NULL," \"\n\t\r,.-;!"); //continue from where you stopped at the text searching for words
- }
- }
- void sort_words(){
- int i, j;
- char word[20]; //temporary word for swapping
- for(i = 0; i < word_count; i++){
- for(j = i; j < word_count; j++){
- if( strcmp(words[i], words[j]) > 0 ){ //when it returns > 0 it means first one is higher alphabetically
- //swap using temporary variable
- strcpy(word, words[i]);
- strcpy(words[i], words[j]);
- strcpy(words[j], word);
- }
- }
- }
- }
- void print_words_to_file(){
- FILE *fp; //file pointer
- int i;
- char word[20]; //temporary word for editing
- fp = fopen("words.txt", "w"); //open file for writing
- for(i = 0; i < word_count; i++){
- strcpy(word, words[i]);
- if(words[i][0] >= 'a') word[0] = word[0] - 32; //if first letter small then make it cap with -32
- 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
- fprintf(fp, "%s\n", word); //write word to file
- }
- fclose(fp); //close file
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement