Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- long_text א. התוכנית מקבלת מהמשתמש טקסט ארוך ושומרת אותו במערך
- [עם הזיכרון המינימלי שנחוץ לשם כך]
- מהמשתמש n ב. התוכנית מקבלת מספר
- מילים n ג. התוכנית קולטת מהמשתמש
- [עם הזיכרון המינימלי שנחוץ לשם כך]
- כל מילה חייבת להכיל לפחות 2 תווים
- כל מילה תהיה מילה אחת, לא יותר ובלי רווחים
- אסור לאותה מילה להופיע פעמיים
- long_text ד. התוכנית מדפיסה כמה פעמים מופיעה כל מילה במערך
- לשם כך עשה שימוש בהקצאת זיכרון הדינאמי
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int len_of_long_text;
- char* From_Big_String_To_dynamic_String(){
- char get_a_big_string[2000], *Pstring;
- flushall();
- do{
- gets(get_a_big_string);
- len_of_long_text = strlen(get_a_big_string);
- Pstring = (char*) malloc ((len_of_long_text+1) * sizeof(char)); // המסיים NULL מוסיפים 1 עבור ה
- if (Pstring == NULL) { printf("\nNo enough memory!.\nEnter again:\n"); continue; } // הבדיקה הזו והלולאה הם רק למקרי קיצון
- if(len_of_long_text == 0) { printf("Please type at least one character.\nEnter again:\n"); continue;}
- break;
- }while(1);
- strcpy(Pstring, get_a_big_string);
- return Pstring;
- }
- int How_many_times_the_word_appear(const char *word, const char *long_tex){
- int sum = 0, i, j, word_len;
- word_len = strlen(word);
- if(word_len>len_of_long_text) return 0;
- for(i=0; i<len_of_long_text; i++){
- for(j=0; j<word_len && j+i<len_of_long_text && word[j] == long_tex[i+j]; j++);
- if(j==word_len) sum++;
- }
- return sum;
- }
- int This_word_already_exists(char tmpStr[50], const char **Array_of_words){ // C זו אמורה להיות פונקציה בוליאנית, אבל אנחנו משתמשים בשפת
- int i;
- for(i=0; Array_of_words[i]; i++){
- if(How_many_times_the_word_appear(tmpStr, Array_of_words[i])==1)
- return 1;
- }
- return 0;
- }
- void From_Big_word_To_dynamic_word(int num, const char **Array_of_words){
- char tmpStr[50], *Pstring;
- int len, i;
- flushall();
- do{
- printf("%d. Enter word: ", num+1);
- gets(tmpStr);
- len = strlen(tmpStr);
- Pstring = (char*) malloc ((len+1) * sizeof(char));
- if (Pstring == NULL) { printf("\nNo enough memory!.\nEnter again:\n"); continue; } // הבדיקה הזו והלולאה הם רק למקרי קיצון
- if(len <= 1) { printf("Please type at least two character.\nEnter again:\n"); continue;}
- for(i=0; i<len; i++){
- if(tmpStr[i] == ' ' || tmpStr[i] == '\t' || tmpStr[i] == '\n'){
- printf("Please type only one word.\nEnter again:\n");
- break; // for-זה מוציא אותנו רק מחוץ ללולאת ה
- }
- }
- if(i!=len) continue; // כאן נגמרת בדיקת התווים
- if(This_word_already_exists(tmpStr, Array_of_words)==1){ // בודק שמילה לא מופיעה כבר
- printf("This word already exists.\nEnter again:\n");
- continue;
- }
- break;
- }while(1);
- strcpy(Pstring, tmpStr);
- Array_of_words[num] = Pstring;
- }
- void main(){
- char *P_long_text, **Array_of_words;
- int n, i, *The_sum_of_occurrences_of_each_word;
- printf("Enter the long text:\n");
- P_long_text = From_Big_String_To_dynamic_String();
- do{
- printf("Enter the number of words you want to search: ");
- scanf("%d", &n);
- }while(n<=0);
- The_sum_of_occurrences_of_each_word = (int*) malloc (n * sizeof(int));
- Array_of_words = (char**) calloc (n , sizeof(char*)); /* "This_word_already_exists()" בכוונה כאן יש קאלוק ולא מאלוק, עבור הפונקציה */
- for(i=0; i<n; i++){
- From_Big_word_To_dynamic_word(i, Array_of_words);
- The_sum_of_occurrences_of_each_word[i] = How_many_times_the_word_appear(Array_of_words[i], P_long_text);
- }
- printf("\n\n\n---Results:\n");
- for(i=0; i<n; i++){
- printf("Word number %d (\"%s\") appears in the text:\n%d times\n\n", i+1, Array_of_words[i], The_sum_of_occurrences_of_each_word[i]);
- }
- {
- free(P_long_text);
- for(i=0; i<n; i++){
- free(Array_of_words[i]);
- }
- free(Array_of_words);
- free(The_sum_of_occurrences_of_each_word);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement