amermo

Najveci broj iz stringa

Feb 14th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. /*Funkcija vraca najveci broj iz stringa, radi i za negativne brojeve! */
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <math.h>
  9.  
  10. int maksi(char* string)
  11. {
  12.     char* p = string;
  13.     int max = pow(-2, 31);
  14.     int broj;
  15.     int minus;
  16.     while(*p != '\0')
  17.     {
  18.         if(*p == '-')
  19.         {
  20.             minus = 1;
  21.             p++;
  22.         }
  23.         if(*p >= '0' && *p <= '9')
  24.         {
  25.             broj = 0;
  26.             while(*p >= '0' && *p <= '9' && *p != '\0')
  27.             {
  28.                 broj*=10;
  29.                 broj+=(int)(*p - '0');
  30.                 p++;
  31.             }
  32.             if(minus == 1)
  33.                 broj*=(-1);
  34.             if(broj > max)
  35.                 max=broj;
  36.         }
  37.         p++;
  38.         minus = 0;
  39.     }
  40.     return max;
  41. }
  42.  
  43.  
  44. int main()
  45. {
  46.     char string[100] = "-11111 ,,,,aaaa-1231, -111 -01";
  47.     printf("Najveci broj je %d", maksi(string));
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment