Advertisement
Guest User

[C] Zalko

a guest
Jan 27th, 2020
1,825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6.  
  7. int sum_greater(const int a, const int b, const int c);
  8. int find_first_A(const char string[]);
  9. char last_letter(const char string[]);
  10. int min_2d(const int size, const int array[][size]);
  11. int is_vowel(const char c);
  12. int is_hex_digit(const char c);
  13. int last(const int size, const int array[]);
  14. int sum_positives(const int size, const int array[]);
  15. int count_zeroes_2d(const int size, int array[][size]);
  16. int n_fib(const int n);
  17. int longest_row(const int rows, const int cols, char array[rows][cols]);
  18. char last_special(const char string[]);
  19. int negative_even(const int number);
  20. void change_whites(char string[]);
  21. void sort_desc(int array[], const int size);
  22. int is_won(const char a, const char b, const char c);
  23. int direction_correction(const int degree);
  24. int sum_digits(const int long number);
  25. int count_negative_evens(const int size, const int array[]);
  26. int is_in_array(const int num, const int size, const int array[]);
  27. int leap_year(const int year);
  28. void digits_sum_sequence(const int size, int array[]);
  29. void swap_sign_2d(const int size , int array[][size]);
  30. void swap_case_2d(const int rows,const int cols, char strings[][cols]);
  31. int vowels_count_2d(const int rows, const int cols, char strings[][cols]);
  32. int is_in_array_2d(const int num, const int size, int array[][size]);
  33. int binary_num(const int num);
  34. int is_special(const char c);
  35. int check_SPZ(const char spz[]);
  36. void fib_2_array(const int size, int array[]);
  37. void string_to_upper(char string[]);
  38. int karel_asleep(const char position);
  39. int bracket_balance(char text[]);
  40. int find_closest_upper_multiple(int factor, int base);
  41.  
  42. int main(){
  43.  
  44. printf("%d\n", sum_greater(-1, 3, -2));
  45. printf("%d\n", find_first_A("Tomorrow afternoon"));
  46.  
  47. printf("%c %c\n", last_letter("Once upon a time...."), last_letter("$a_b_c_d_1_2_3_4$"));
  48.  
  49. int array[2][2] = { {1, 2}, {0, -3} };
  50. printf("%d\n", min_2d(2, array));
  51.  
  52. printf("%d %d %d\n", is_vowel('b'), is_vowel('A'), is_vowel('a'));
  53.  
  54. printf("%d %d\n", is_hex_digit('a'), is_hex_digit('F'));
  55. printf("%d %d\n", is_hex_digit('h'), is_hex_digit('3'));
  56.  
  57. int arrayA[3] = {1,2,3};
  58. printf("%d\n", last(3, arrayA));
  59.  
  60. int arrayB[] = {0,1,-2,3};
  61. printf("%d\n", sum_positives(4, arrayB));
  62.  
  63. int arrayC[2][2] = { {1,2}, {0,0} };
  64. printf("%d\n", count_zeroes_2d(2, arrayC));
  65.  
  66. printf("%d\n", n_fib(10));
  67.  
  68. char arrayD[3][31] = {"Hello",
  69.                      "Hello, how are you?",
  70.                      "I hope today is a lucky day..."};
  71. printf("%d\n", longest_row(3,31,arrayD));
  72.  
  73. printf("%c %c\n", last_special("Once upon a time....."), last_special("$a_b_c_d_1_2_3_4$"));
  74.  
  75. printf("%d %d %d\n", negative_even(-10), negative_even(-11), negative_even(10));
  76.  
  77. char str[] = "Hello world!";
  78. change_whites(str);
  79. printf("%s\n", str);
  80.  
  81.  
  82. int arrayE[5] ={10,20,50,30,40};
  83. sort_desc(arrayE, 5);
  84. for(int i = 0; i < 5; i++){
  85.     printf("%d", arrayE[i]);
  86. }
  87.  
  88.  
  89. printf("%d %d\n", is_won('y', 'Y', 'y'), is_won('a', 'B', 'B'));
  90.  
  91.  
  92. printf("%d %d %d\n", direction_correction(-90), direction_correction(540), direction_correction(180));
  93.  
  94. //printf("%d %d\n", sum_digits(0), sum_digits(15));
  95.  
  96.  
  97.  
  98.  
  99. return 0;
  100.  
  101. }
  102.  
  103. /////////////////////////////////////////////////////////////////////////////////////////////////////
  104.  
  105. int sum_greater(const int a, const int b, const int c){
  106.  
  107. if(a > c && a > b){
  108.     return a + c;
  109.  
  110. }
  111. else{
  112.     return a + b;
  113. }
  114. if(b > a && b > c){
  115.     return b + a;
  116. }
  117. else{
  118.     return b + c;
  119. }
  120. if(c > a && c > b){
  121.     return c + a;
  122. }
  123. else{
  124.     return c + b;
  125. }
  126.  
  127. }
  128.  
  129. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  130.  
  131. int find_first_A(const char string[]){
  132.  
  133.   if(string == NULL){
  134.       return -1;
  135.  
  136. }
  137.   for(int i = 0; i < strlen(string); i++){
  138.     if(string[i] == 'A' || string[i] == 'a'){
  139.         return i;
  140.     }
  141.   }
  142.  
  143.     if(string != 'A' || string != 'a'){
  144.         return -1;
  145.     }
  146.  
  147. }
  148.  
  149. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  150.  
  151. char last_letter(const char string[]){
  152.  
  153.  if(string == NULL){
  154.     return '?';
  155.  }
  156.  
  157. char pos;
  158.  
  159.  for(int j = 0; j < strlen(string); j++){
  160.      if(string[j] >= 'A' && string[j] <= 'Z' || string[j] >= 'a' && string[j] <= 'z'){
  161.         pos = string[j];
  162.      }
  163.  }
  164.  if((pos >= 'a' && pos <= 'z') ||(pos >= 'A' && pos <= 'Z')){
  165.     return pos;
  166.  }
  167.  else{
  168.     return '?';
  169.  }
  170.  
  171. }
  172.  
  173. //////////////////////////////////////////////////////////////////////////////////////////////////////
  174.  
  175. int min_2d(const int size, const int array[][size]){
  176.     int min ;
  177.  
  178.  
  179.     if(min == NULL){
  180.         return -1;
  181. }
  182.  
  183. for(int i = 0; i < size; i++){
  184.     for(int j = 0; j < size; j++){
  185.          if(min > array[j][i])
  186.             min = array[j][i];
  187.  
  188.     }
  189. }
  190.  
  191. return min;
  192. }
  193.  
  194. ///////////////////////////////////////////////////////////////////////////////////////////////////
  195.  
  196. int is_vowel(const char c){
  197.     if(c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'){
  198.         return 1;
  199.     }
  200.     if(c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U'){
  201.         return 1;
  202.     }
  203.     else{
  204.         return 0;
  205.     }
  206.  
  207.  
  208.  
  209. }
  210.  
  211. \\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  212.  
  213. int is_hex_digit(const char c){
  214.  
  215.     if(c >= 'A' && c <= 'F'){
  216.         return 1;
  217.     }
  218.     if(c >= '0' && c <= '9'){
  219.         return 1;
  220.     }
  221.   if(c >= 'a' && c <= 'f'){
  222.           return 1;
  223.     }
  224.  
  225.     return 0;
  226. }
  227. int last(const int size, const int array[]){
  228.     int i;
  229.  
  230.     for(int i = 0; i < size; i++){
  231.         if(i == ' ' || i == '.'){
  232.             return i;
  233.         }
  234.     }
  235.  
  236. }
  237.  
  238. ///////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  239.  
  240. int sum_positives(const int size, const int array[]){
  241.  
  242. if(array == NULL){
  243.     return -1;
  244. }
  245.  
  246.  
  247. int counter = 0;
  248.  
  249. for(int i = 0; i < size; i++){
  250.     if(array[i] >= 0){
  251.         counter += array[i];
  252.     }
  253. }
  254.     return counter;
  255. }
  256.  
  257. ///////////////////////////////////////////////////////////////////////////////////////////////////
  258.  
  259. int count_zeroes_2d(const int size, int array[][size]){
  260.  
  261.  if(array == NULL){
  262.      return -1;
  263. }
  264.  
  265. int counter = 0;
  266.  
  267. for(int i = 0; i < size; i++){
  268.     for(int j = 0; j < size; j++){
  269.         if(array[i][j] == 0){
  270.             counter++;
  271.         }
  272.  
  273.  
  274.     }
  275. }
  276.     return counter;
  277.  
  278. }
  279.  
  280. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  281.  
  282. int n_fib(const int n){
  283.  
  284.     int fib = 0;
  285.     int fib1 = 0;
  286.     int fib2 = 1;
  287.  
  288.  
  289.     if(n < 1 || n > 575){
  290.         return -1;
  291.     }
  292.  
  293.     for(int i = 2; i <= n ; i++){
  294.  
  295.         fib= fib1 + fib2;
  296.          fib1 = fib2;
  297.           fib2 = fib;
  298.  
  299.     }
  300.     return fib;
  301. }
  302.  
  303. ////////////////////////////////////////////////////////////////////////////////////////
  304.  
  305. int longest_row(const int rows, const int cols, char array[rows][cols]){
  306.     int result = 0;
  307.     int longest = 0;
  308.      for(int y = 0; y < rows; y++){
  309.         int len = strlen(array[y]);
  310.         if(result < len){
  311.             result = len;
  312.             longest = y;
  313.         }
  314.     }
  315.  
  316.     return longest;
  317. }
  318.  
  319. ////////////////////////////////////////////////////////////////////////////////////////
  320.  
  321. char last_special(const char string[]){
  322.  
  323. if(string == NULL){
  324.     return '?';
  325. }
  326.  
  327. char pos;
  328.  
  329. for(int i = 0; i < strlen(string); i++){
  330.     if(string[i] != 'a' && string[i] != 'z' || string[i] != 'A' && string[i] != 'Z' ){
  331.         pos = string[i];
  332.     }
  333.     if(string[i] != '0' && string[i] != '9'){
  334.         pos = string[i];
  335.     }
  336.  
  337. }
  338.  
  339. return pos;
  340.  
  341. }
  342.  
  343. ////////////////////////////////////////////////////////////////////////////////////////
  344.  
  345.  
  346. int negative_even(const int number){
  347.  
  348. if(number % 2 == 0 && number < 0){
  349.     return 1;
  350. }
  351.     else{
  352.         return 0;
  353.     }
  354.  
  355. }
  356.  
  357. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  358.  
  359. void change_whites(char string[]){
  360.  
  361. if(string == NULL){
  362.     return;
  363. }
  364. for(int i = 0; i < strlen(string); i++){
  365.     if(string[i] == ' ' || string[i] == '\n'){
  366.         string[i] = '.';
  367. }
  368. }
  369. }
  370.  
  371.  
  372. ////////////////////////////////////////////////////////////////////////////////////////
  373.  
  374. void sort_desc(int array[], const int size){
  375.  
  376. if(array == NULL || size < 1){
  377.     return;
  378. }
  379.  
  380. int a;
  381.  
  382.  
  383. for(int i = 0; i < size; i++){
  384.    for(int j = i; j < size; j++){
  385.     if(array[i] < array[j]){
  386.         a = array[i];
  387.         array[i] = array[j];
  388.         array[j] = a;
  389.     }
  390.    }
  391. }
  392.  
  393. }
  394.  
  395. ////////////////////////////////////////////////////////////////////////////////////////
  396.  
  397.  
  398. int is_won(const char a, const char b, const char c){
  399.     char a1 = tolower(a);
  400.     char b1 = tolower(b);
  401.     char c1 = tolower(c);
  402.     if((a1 == b1) && (b1 == c1) && (a1 == c1)){
  403.         return 1;
  404.     }
  405.     else{
  406.         return 0;
  407.     }
  408. }
  409.  
  410. /////////////////////////////////////////////////////////////////////////////
  411.  
  412. int direction_correction(const int degree){
  413.     //  int direct = degree;
  414.     if(degree > 0 && degree < 360)
  415.         return degree;
  416.     else if(degree > 360)
  417.         return degree - 360;
  418.     else if(degree < 0)
  419.         return -1;
  420. }
  421.  
  422. ////////////////////////////////////////////////////////////////////////////////////////
  423.  
  424. int count_negative_evens(const int size, const int array[]){
  425.  if(array == NULL){
  426.     return -1;
  427.  }
  428.  
  429.  
  430.  int counter = 0;
  431.  for(int i = 0; i < size; i++){
  432.      if(array[i] < 0 && array[i] % 2 == 0){
  433.         counter ++;
  434.      }
  435.  }
  436.     return counter;
  437.  
  438. }
  439.  
  440. ////////////////////////////////////////////////////////////////////////////////////////
  441.  
  442. int is_in_array(const int num, const int size, const int array[]){
  443.  
  444. if(array == NULL){
  445.     return -1;
  446. }
  447.  
  448. for(int i = 0; i < size; i++){
  449.     if(num == array[i]){
  450.         return 1;
  451.     }
  452.  
  453. }
  454. return 0;
  455. }
  456.  
  457. ////////////////////////////////////////////////////////////////////////////////////////
  458.  
  459. int leap_year(const int year){
  460.  
  461. if(year < 1 || year > 4443){
  462.     return -1;
  463. }
  464.  
  465.  
  466. if (year % 4 == 0) {
  467.         if (year % 100 == 0) {
  468.  
  469.             if (year % 400 == 0)
  470.                 return 1;
  471.             else
  472.                 return 0;
  473.         } else
  474.             return 1;
  475.     } else
  476.         return 0;
  477.  
  478.  
  479. }
  480.  
  481. ///////////////////////////////////////////////////////////////////////////////////
  482.  
  483.  
  484. void digits_sum_sequence(const int size, int array[]){
  485.    if(array == NULL || size == 0){
  486.     return;
  487.    }
  488.    array[0] = 1;
  489.    int sum = 0;
  490.    for(int i = 1; i<size; i++){
  491.     sum = sum + array[i-1];
  492.     array[i] = sum;
  493.    }
  494. }
  495.  
  496. /////////////////////////////////////////////////////////////////////////////
  497.  
  498. void swap_sign_2d(const int size, int array[2][2]){
  499.     if(array[size][size] == NULL){
  500.         return;
  501.     }
  502.  
  503.     for(int i = 0; i < size; i++){
  504.         for(int j = 0; j < size; j++){
  505.             if(array[j][i] > 0){
  506.                 array[j][i] = (-1)*array[j][i];
  507.             }
  508.             else if(array[j][i] < 0){
  509.                 array[j][i] = (-1)*array[j][i];
  510.             }
  511.             else if(array [j][i] == 0){
  512.                 array[j][i] = 0;
  513.             }
  514.  
  515.          //   array[j][i] = array[j][i] - 2*array[j][i];
  516.         }
  517.  
  518.     }
  519. }
  520.  
  521. /////////////////////////////////////////////////////////////////////
  522.  
  523. void swap_case_2d(const int rows,const int cols, char strings[][cols]){
  524.     int word = 0;
  525.     for(int y = 0; y < rows; y++){
  526.         for(int x = 0; x < cols; x++){
  527.             if(islower(strings[y][x]))
  528.                 strings[y][x]=toupper(strings[y][x]);
  529.             else if(isupper(strings[y][x]))
  530.                 strings[y][x]=tolower(strings[y][x]);
  531.  
  532.         }
  533.     }
  534.  
  535.  
  536. }
  537.  
  538. ////////////////////////////////////////////////////////////////////////
  539.  
  540.  
  541. int vowels_count_2d(const int rows, const int cols, char strings[][cols]){
  542.  
  543.    if(strings == NULL){
  544.     return -1;
  545.    }
  546.     int counter = 0;
  547.      for(int y = 0; y < rows; y++){
  548.         for(int x = 0; x < cols; x++){
  549.         if(strings[y][x] == 'a' || strings[y][x]  == 'e' || strings[y][x]  == 'i' || strings[y][x]  == 'o' || strings[y][x]  == 'u' || strings[y][x]  == 'y'){
  550.             counter = counter +1;
  551.         }
  552.         else if(strings[y][x]  == 'A' || strings[y][x]  == 'E' || strings[y][x]  == 'I' || strings[y][x]  == 'O' || strings[y][x]  == 'U' || strings[y][x]  == 'Y'){
  553.                 counter = counter+1;
  554.         }
  555.  
  556.  
  557.      }
  558.  
  559. }
  560.     return counter;
  561. }
  562.  
  563. ////////////////////////////////////////////////////////////////////
  564.  
  565. int is_in_array_2d(const int num, const int size, int array[][size]){
  566.     for(int y = 0; y < size; y++){
  567.         for(int x = 0;x < size; x++){
  568.             if(array[y][x] == num){
  569.                 return 1;
  570.             }
  571.  
  572.         }
  573.     }
  574.     return 0;
  575.  
  576. }
  577.  
  578. /////////////////////////////////////////////////////////////////////////////
  579.  
  580. int count_whites(const char string[]){
  581.     char text = string;
  582.     int counter = 0;
  583.     int index = 0;
  584.    
  585.     while(text[index] != '\0'){
  586.         if(text[index] == NULL)
  587.             return -1;
  588.         else if(text[index] == ' ' || text[index] == '\t' || text[index] == '\n'){
  589.                 counter += 1;
  590.                 index++;
  591.         }
  592.         index++;
  593.        // char str[] = "Geeks";
  594.     }
  595.    
  596.     return counter;
  597. }
  598.  
  599. /////////////////////////////////////////////////////////////////////////////////
  600.  
  601. int binary_num(const int num){
  602.  
  603.  if(num == 1 || num == 0){
  604.     return 1;
  605.  }
  606.      else if(num < -1000 || num > 1000 ){
  607.         return -1;
  608.      }
  609.     else{
  610.         return 0;
  611.     }
  612. }
  613.  
  614. /////////////////////////////////////////////////////////////////////////////////
  615.  
  616. int is_special(const char c){
  617.     if(c >= '0' && c <= '9'){
  618.         return 0;
  619. }
  620.         else if(c >= 'A' && c <= 'Z'){
  621.                 return 0;
  622. }
  623.                 else if(c >= 'a' && c <= 'z'){
  624.                         return 0;
  625. }
  626.  
  627.  
  628.     return 1;
  629. }
  630.  
  631. /////////////////////////////////////////////////////////////////////////
  632.  
  633. int check_SPZ(const char spz[]){
  634.  
  635.     for(int i = 0; i < strlen(spz); i++){
  636.             if(strlen(spz) != 7 || spz[0] > 'Z' || spz[0] < 'A' || spz[1] > 'Z' || spz[1] < 'A' ||
  637.                 spz[2] > '9' || spz[2] < '0'|| spz[3] > '9' || spz[3] < '0' || spz[4] > '9' || spz[4] < '0'
  638.                 || spz[5] > 'Z' || spz[5] < 'A' || spz[6] > 'Z' || spz[6] < 'A'){
  639.                 return 0;
  640.             }
  641.     }
  642.  
  643.  
  644.     return 1;
  645. }
  646.  
  647. ///////////////////////////////////////////////////////////////////////////////
  648.  
  649. void fib_2_array(const int size, int array[]){
  650.     if(array == NULL){
  651.         return;
  652.     }
  653.        array[0] = 1;
  654.        array[1] = 1;
  655.  
  656.     for(int i = 2; i < size; i++){
  657.         array[i] = array[i - 2] + array [i - 1];
  658.        }
  659.  }
  660.  
  661. ///////////////////////////////////////////////////////////////////////////////
  662.  
  663. void string_to_upper(char string[]){
  664.     int n=strlen(string);
  665.     for(int i = 0; i < n; i++){
  666.         if(islower(string[i])){
  667.             string[i] = toupper(string[i]);
  668.         }
  669.     }
  670. }
  671.  
  672. //////////////////////////////////////////////////////////////////////////////////////////////
  673.  
  674. int karel_asleep(const char position){
  675.  
  676.     if(position == '-'){
  677.        return 1;
  678.     }
  679.     if(position == '|'){
  680.       return 0;
  681.     }
  682. return -1;
  683. }
  684.  
  685. ////////////////////////////////////////////////////////////////////////////
  686.  
  687. #include <stdio.h>
  688. #include <stdlib.h>
  689.  
  690. int main()
  691. {
  692.     int n;
  693.     scanf("%d", &n);
  694.      int array[n];
  695.      int pom;
  696.      for(int i = 0; i < n; i++){
  697.         scanf("%d",&pom);
  698.         array[i] = pom;
  699.         }
  700.      for(int i = 0; i < n; i++){
  701.         for(int j = i; j < n; j++){
  702.             if(array[i] > array[j]){
  703.                 pom = array[i];
  704.                 array[i] = array[j];
  705.                 array[j] = pom;
  706.  
  707.             }
  708.         }
  709.         printf("Z:%d\n", array[i]);
  710.      }
  711.      if(n % 2 == 0){
  712.         float median = ((float)array[(n - 1) / 2] + (float)array[n / 2]) / 2;
  713.         printf("Median je: %f\n", median);
  714.      }
  715.      else{
  716.         printf("Median je : %d\n", array[n/2]);
  717.      }
  718.     return 0;
  719. }
  720.  
  721. //////////////////////////////////////////////////////////////////////////////////
  722.  
  723. int bracket_balance(char text[]){
  724.     int n = strlen(text);
  725.     int j=0, k=0;
  726.     if(strlen(text) == 0){
  727.         return 0;
  728.     }
  729.     for(int i =0; i<n; i++){
  730.         if(text[i] == '('){
  731.             j++;
  732.         }
  733.         if(text[i] == ')'){
  734.             k++;
  735.         }
  736.     }
  737.     int v = j-k;
  738.    
  739.     return v;
  740. }
  741.  
  742. //////////////////////////////////////////////////////////////////////////////////
  743.  
  744. int find_closest_upper_multiple(int factor, int base){
  745.     int n = factor;
  746.     if(factor < 0 || base < 0){
  747.         return 0;
  748.     }
  749.     while(n <= base){
  750.         n = n + factor;
  751.     }
  752.     return n;
  753. }
  754.  
  755. ///////////////////////////////////////////////////////////////////////////////////
  756.  
  757. int sum_digits(const long int number){
  758.     int sum = 0;
  759.     int c = number;
  760.     if(c < 0){
  761.         return 0;
  762.     }
  763.     while (c > 0){
  764.         sum += (c % 10);
  765.         c = c / 10;
  766.     }
  767.     return sum;
  768. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement