Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///\file proiect.c
- ///\brief C Text editor library
- ///
- /// Created by Badea Elena-Delia
- /// Implements functions for determinating if a word is present in a document, determinating the number of occurrences for a word in a document, replacing all instances of a word in a document with another given word, replacing specific instances of a word in a document with another given word, creating a sorted list of words by the number of occurrences in a document.
- #include<stdlib.h>
- #include<string.h>
- #include<stdio.h>
- #include<malloc.h>
- ///The variables ok and current_number are declared and initialized outside the functions
- int ok = 0 ;
- int current_number = 0;
- int word(char given_word[256], char text_from_file[256])
- {
- ///\fn word(char given_word[256], char text_from_file[256])
- ///\brief This function is used to determinate if a word is present in a document, but it also counts the number of occurences of that word
- ///\param given_word The word we are looking for
- ///\param text_from_file The text from our file
- char *cuv;
- ///We start by dividing the text into words, using the strtok function
- cuv = strtok(text_from_file, " ,.!?;");
- while(cuv)
- {
- if(strcmp(given_word, cuv)==0)
- {
- ///Every time we found the word, we increment the variable ok
- ok++;
- }
- cuv = strtok(NULL," ,.!?;" );
- }
- ///We return the number of appearences
- return ok;
- }
- char *replace(const char *s, const char *old, const char *new)
- {
- ///\fn char *replace(const char *s, const char *old, const char *new)
- ///\brief This function is used to replace every all instances of a word in our file with another one
- ///\param s The text where we are looking for the word and we replace it
- ///\param old The word we want to be replaced
- ///\param new The word we want to replace with
- char *ret;
- int i, count = 0;
- ///The lenght of the new word
- size_t newlen = strlen(new);
- ///The lenght of the old word (the word we want to replace)
- size_t oldlen = strlen(old);
- for (i = 0; s[i] != '\0'; i++)
- {
- ///We go through the text and we are looking for our word
- if (strstr(&s[i], old) == &s[i])
- {
- ///When we found the word, we increase the paramer count, used when we allocate memory for the new word
- count++;
- i += oldlen - 1;
- }
- }
- ///We allocate memory for the new word
- ret = malloc(i + count * (newlen - oldlen));
- if (ret == NULL)
- {
- exit(EXIT_FAILURE);
- }
- i = 0;
- while (*s)
- {
- if (strstr(s, old) == s)
- {
- ///Here, the replacing is made, using the function strcpy, as long as we found the word in the text
- strcpy(&ret[i], new);
- i += newlen;
- s += oldlen;
- }
- else
- {
- ret[i++] = *s++;
- }
- }
- ret[i] = '\0';
- return ret;
- }
- void new_word(FILE *f_2, char given_word[256], char rpwrd[256], char text_from_file[256])
- {
- ///\fn new_word(FILE *f_2, char given_word[256], char rpwrd[256], char text_from_file[256])
- ///\brief In this function we call the function replace and we replace the given word with another word in our text
- ///To do this, first we copy the text from the file in a variable, named here text_2. We do this because our text suffers changes and it can produce some errord to other functions
- ///Then we copy, using strcpy, in the text_2, the new text (made by calling the function replace) and after that we printf it in the file.
- ///\param f_2 Pointer to the file where we print the new text
- ///\param given_word The word we want to be replace
- ///\param rpwrd The word that we want to replace the given_word with
- ///\param text_from_file The text from our file
- char text_2[256];
- strcpy(text_2, text_from_file);
- strcpy(text_2,replace(text_2, given_word, rpwrd) );
- fprintf(f_2, "%s" ,text_2);
- }
- char *replace_one_word(const char *s, const char *old, const char *new, int word_number)
- {
- ///\fn char *replace_one_word(const char *s, const char *old, const char *new, int word_number)
- ///\brief This function is like the function replace, but here, we replace only a specific instances of a word with another given word
- ///To do this, we count the appearences of the word and we replace only the specific inatance
- ///\param s The text
- ///\param old The word we want to be replaced
- ///\param new The word we want to replace with
- ///\param word_number The number of the appearance of the word that we want to replace
- char *ret;
- int i, count = 0;
- size_t newlen = strlen(new);
- size_t oldlen = strlen(old);
- for (i = 0; s[i] != '\0'; i++)
- {
- if (strstr(&s[i], old) == &s[i])
- {
- count++;
- i += oldlen - 1;
- }
- }
- ret = malloc(i + (newlen - oldlen));
- if (ret == NULL)
- {
- exit(EXIT_FAILURE);
- }
- i = 0;
- while (*s)
- {
- if (strstr(s, old) == s )
- {
- current_number++;
- if(current_number == word_number)
- {
- ///We only replace the word when we are on the word_number-th appearence
- strcpy(&ret[i], new);
- i += newlen;
- s += oldlen;
- }
- else{ret[i++] = *s++;}
- }
- else
- {
- ret[i++] = *s++;
- }
- }
- ret[i] = '\0';
- return ret;
- }
- void replace_a_word(FILE *f, char given_word[256], char rpwrd[256], char text_from_file[256], int nr)
- {
- ///\fn replace_a_word(FILE *f, char given_word[256], char rpwrd[256], char text_from_file[256], int nr)
- ///\brief This function is used to replace a specific instance of a word with another given one
- ///To do this, we copy, using the function strcpy, our text from the file in the variable text, and then, using the function replace_one_word, we copy in variable text the new text obtained by calling the function replace_one_word
- ///Then we print the new text in the file.
- ///\param f Pointer to the file
- ///\param given_word The word we want to be replace
- ///\param rpwrd The word that we want to replace the given_word with
- ///\param text_from_file The text from our file
- ///\param nr The number of the appearence of the word we want to replace
- char text[256];
- strcpy(text, text_from_file);
- strcpy(text,replace_one_word(text, given_word, rpwrd, nr) );
- fprintf(f, "%s" ,text);
- }
- void sort(int *a[100], int *b[100], int n)
- { ///\fn void sort(int a[100], char *b[100])
- ///\brief This function is used to sort the two arrays, based on array a
- ///\param a A random array
- ///\param b A random array of strings
- int i,j, aux_a, aux_b;
- for(i=0; i<n; i++)
- {
- for(j=0; j<n; j++)
- {
- if(a[i] > a[j])
- {
- aux_a = a[i]; ///Interchange in a
- a[i] = a[j];
- a[j] = aux_a;
- aux_b = b[i];
- b[i] = b[j];
- b[j] = aux_b;
- }
- }
- }
- }
- void cuv(FILE *f)
- { ///\fn void cuv(FILE *f)
- ///\brief This function is used to list the words from the file sorted by the number of their occurences in the text
- ///\param f File pointer
- int lenght = 0, occure[100], i , j, freq[100], k , ok;
- char *words, text[256], *words_array[256], *list[256];
- while(!(feof(f)))
- {
- fgets(text, sizeof(text),f);
- words = strtok(text, " ,.!?;\n\r\t");
- while(words)
- {
- words_array[lenght] =(char*)calloc(words, sizeof(words) + 1); ///store the words into an string array
- strcat(words_array[lenght], words);
- lenght++;
- words = strtok(NULL," ,.!?;\n\r\t" );
- }
- }
- for(i=0;i<lenght;i++)
- { k=0;
- for(j=0;j<lenght;j++)
- {
- if(strcmp(words_array[i], words_array[j]) == 0)
- {
- occure[i] = k+1; ///count the appearences
- k++;
- }
- }
- freq[i] = i; ///memorize the position
- }
- sort(occure, freq, lenght);
- j = 0 ;
- printf("List of words: ");
- ok = 0;
- while(j<lenght)
- {
- printf("%s ", words_array[freq[j]]); ///We print the words sorted by their number of occurence
- j++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement