KvArt

Kompletan zadatak sa svim funkcijama za manipulaciju unetog teksta

Jul 12th, 2022 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. */Program sa funkcijama za unos teksta, podelu teksta na stringove po recima, pronalazenje najkrace reci i brisanje, pronalazenje kljucne reci i brisanje.
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define WORD_LEN 100
  6.  
  7. /*• Napisati funkciju char** load_words() koja učitava jedan red teksta sa standardnog ulaza i smešta ga kao niz reči koji vraća kao rezultat. Red se sastoji samo od reči i tačno jednog blanko
  8. znaka između svake dve susedne reči. Na početku, kao i na kraju reda su reči. Za učitavanje
  9. teksta je potrebno dinamički alocirati prostor tako da se tekst smesti kao niz reči. Smatrati da reč ima najviše 100 znakova. Na kraju izvršavanja prethodne funkcije treba da bude alocirano tačno onoliko prostora koliko je potrebno da se smeste učitane reči.*/
  10.  
  11. char** load_words()
  12. {
  13. char c, **words = NULL;
  14. int wordCnt = 0, wordStart = 1, pos;
  15. while(1)
  16.     {
  17.     c = getchar();
  18.     if(c == '\n' && !wordCnt) break;
  19.     if(wordStart)
  20.         {
  21.             words = realloc(words,
  22.             sizeof(*words) * (++wordCnt + 1));
  23.             if(!words)
  24.                 {
  25.                     perror(NULL); exit(EXIT_FAILURE);
  26.                 }
  27.         words[wordCnt - 1] = calloc(sizeof(**words), (WORD_LEN + 1));
  28.         if(!words[wordCnt - 1])
  29.             {
  30.                 perror(NULL); exit(EXIT_FAILURE);
  31.             }
  32.         words[wordCnt] = NULL;
  33.         wordStart = 0; pos = 0;
  34.         }
  35.     words[wordCnt - 1][pos++] = c;
  36.     if(c == ' ' || c == '\n')
  37.         {
  38.             words[wordCnt - 1] = realloc(words[wordCnt - 1],
  39.             sizeof(**words) * pos);
  40.             words[wordCnt - 1][pos-1] = '\0';
  41.             wordStart = 1;
  42.             if(c == '\n') break;
  43.         }
  44.     }
  45. return words;
  46. }
  47.  
  48. //Napisati funkciju char* shortest_word(char** s) koja vraća prvu najkraću reč (string) u nizu reči s.
  49.  
  50. char* shortest_word(char** s)
  51. {
  52.     char *shor_w = NULL;
  53.     while(s && *s)
  54.         {
  55.             if(!shor_w || strlen(*s) < strlen(shor_w))
  56.             shor_w = *s;
  57.             s++;
  58.         }
  59.     if(shor_w)
  60.         {
  61.             char *t=calloc(sizeof(*t),strlen(shor_w)+1);
  62.             strcpy(t, shor_w);
  63.             shor_w = t;
  64.         }
  65.     return shor_w;
  66. }
  67.  
  68. /*Napisati funkciju char** rem_word(char** s, char* w) koja uklanja sve pojave reči w iz niza reči s i vraća pokazivač na niz reči iz kojeg su izbačene pojave reči w.*/
  69.  
  70. char** rem_word(char** s, char* w)
  71. {
  72.     if(!w) return s;
  73.     char **pos = s, **old_s = s;
  74.     while(s && *s)
  75.         {
  76.             if(strcmp(*s, w))
  77.             *pos++ = *s;
  78.             else free(*s);
  79.             s++;
  80.         }
  81.     if(old_s)
  82.         {
  83.             *pos = *s; //NULL
  84.             old_s = realloc(old_s,
  85.             sizeof(*old_s) * (pos - old_s + 1));
  86.         }
  87.     return old_s;
  88. }
  89.  
  90. //• Napisati funkciju void free_words(char** w) koja oslobađa alociranu dinamičku memoriju.
  91.  
  92. void free_words(char **w)
  93. {
  94.     char **t = w;
  95.     while(w && *t)
  96.         {
  97.             free(*t);
  98.             t++;
  99.         }
  100.     free(w);
  101. }
  102.  
  103. int main(void)
  104. {
  105.     char **words = load_words();
  106.     char **w = words;
  107.     while(w && *w)
  108.         {
  109.             printf("%s\n", *w);
  110.             w++;
  111.         }
  112.     char *sw = shortest_word(words);
  113.     printf("Shortest word: %s\n", sw);
  114.     w = rem_word(words, sw);
  115.     free(sw);
  116.     puts("====================");
  117.     while(w && *w)
  118.         {
  119.             printf("%s\n", *w);
  120.             w++;
  121.         }
  122.     free_words(words);
  123. }
Add Comment
Please, Sign In to add comment