Advertisement
lukicdarkoo

Prethodni na drugi nacin

Dec 10th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_WORD_SIZE 100
  5. #define MAX_WORDS 10
  6.  
  7. int main()
  8. {
  9.     char text[MAX_WORD_SIZE];
  10.     char splitted[MAX_WORDS][MAX_WORD_SIZE];
  11.     int splitted_i;
  12.  
  13.     char shortestWord[MAX_WORD_SIZE];
  14.     char longestWord[MAX_WORD_SIZE];
  15.  
  16.     gets(text);
  17.     ld_split(text, splitted, &splitted_i);
  18.  
  19.  
  20.     strcpy(shortestWord, splitted[0]);
  21.     strcpy(longestWord, splitted[0]);
  22.     int i = 1;
  23.     for (; i < splitted_i; i++) {
  24.         if (ld_strlen(splitted[i]) > ld_strlen(longestWord))
  25.             strcpy(longestWord, splitted[i]);
  26.  
  27.         if (ld_strlen(splitted[i]) < ld_strlen(shortestWord))
  28.             strcpy(shortestWord, splitted[i]);
  29.     }
  30.  
  31.     printf("Najduza: \"%s\" (%i)\n", longestWord, ld_strlen(longestWord));
  32.     printf("Najkraca: \"%s\" (%i)\n", shortestWord, ld_strlen(shortestWord));
  33.  
  34.     return 0;
  35. }
  36.  
  37.  
  38. int ld_strlen(char *text) {
  39.     int i = 0;
  40.     while (text[i] != 0)
  41.         i++;
  42.  
  43.     return i;
  44. }
  45.  
  46. void ld_split(char text[MAX_WORD_SIZE], char splitted[MAX_WORDS][MAX_WORD_SIZE], int *splitted_num) {
  47.     int splitted_j = 0, splitted_i = 0;
  48.  
  49.     int i = 0;
  50.     for (; i < ld_strlen(text) + 1; i++) {
  51.         if (text[i] == ' ' || text[i] == 0) {
  52.             splitted[splitted_i++][splitted_j] = 0;
  53.             splitted_j = 0;
  54.         }
  55.  
  56.         else
  57.             splitted[splitted_i][splitted_j++] = text[i];
  58.     }
  59.  
  60.     *splitted_num = splitted_i;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement