Advertisement
anon20016

Untitled

Jan 20th, 2020
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // returns number of max elements in array
  5. int count_max_arr(int * arr, int n){
  6. int i, c = 1;
  7. int mx = arr[0];
  8. for (i = 0; i < n; i++){
  9. if (arr[i] == mx){
  10. c++;
  11. } else{
  12. if (arr[i] > mx){
  13. mx = arr[i];
  14. c = 1;
  15. }
  16. }
  17. }
  18. return c;
  19. }
  20.  
  21. // returns the index of the first occurrence of x in the array
  22. // -1, if not found
  23. int first(int * ax, int x, int n){
  24. int i;
  25. for(i = 0; i < n; i++){
  26. if(ax[i] == x){
  27. return i;
  28. }
  29. }
  30. return -1;
  31. }
  32.  
  33. // returns the index of the last occurrence of x in the array
  34. // -1, if not found
  35. int last(int * ax, int x, int n){
  36. int g = -1, i;
  37. for(i = 0; i < n; i++){
  38. if(ax[i] == x){
  39. g = i;
  40. }
  41. }
  42. return g;
  43. }
  44.  
  45. int len(char * c){ // length of string
  46. int i;
  47. for (i = 0; c[i] != '\0'; i++);
  48. return i;
  49. }
  50.  
  51. // concatenate str and str1, result in str
  52. char concat(char * str, char * str1){
  53. int l = len(str), i;
  54. for(i = 0; str1[i]!='\0'; i++){
  55. str[l] = str1[i];
  56. l++;
  57. }
  58. str[l] = '\0';
  59. return str;
  60. }
  61. // returns string concatenate str and str1
  62. char * concat1(char * str, char * str1){
  63. char * result = malloc(sizeof(str) + sizeof(str1) + 1);
  64. int l = 0, i;
  65. for(i = 0; str[i]!='\0'; i++){
  66. result[l] = str[i];
  67. l++;
  68. }
  69. for(i = 0; str1[i]!='\0'; i++){
  70. result[l] = str1[i];
  71. l++;
  72. }
  73. return result;
  74. }
  75.  
  76. // counts number of char c in string
  77. int count_char(char * str, char c){
  78. int i, k = 0;
  79. for(i = 0; str[i]!='\0'; i++){
  80. if(str[i] == c){
  81. k++;
  82. }
  83. }
  84. return k;
  85. }
  86.  
  87. int count_str_in_str(char *str, char *str1){
  88. int i, j, k = 0;
  89. for(i = 0; i < len(str) - len(str1) + 1; i++){
  90. int f = 1;
  91. for(j = 0; str1[j]!='\0'; j++){
  92. if(str[i + j] != str1[j]){
  93. f = 0;
  94. }
  95. }
  96. if (f == 1){
  97. k++;
  98. }
  99. }
  100. return k;
  101. }
  102.  
  103. int first_find_str_in_str(char * a, char * b){
  104. int i, j, s, x = 0;
  105. for(i = 0; i < len(a) - len(b) + 1; i++){
  106. s = 1;
  107. for(j = 0; b[j]!='\0'; j++){
  108. if(a[i + j] != b[j]){
  109. s = 0;
  110. }
  111. }
  112. if (s == 1){
  113. return i;
  114. }
  115. }
  116. return -1;
  117. }
  118.  
  119. // returns string with x elements c
  120. char * get(int x, char c){
  121. char * a = malloc(100 * sizeof(char));
  122. int i;
  123. for (i = 0; i < x; i++){
  124. a[i] = c;
  125. }
  126. a[i] = '\0';
  127. return a;
  128. }
  129.  
  130. int main(){
  131. char a[100] = "abcd";
  132. char b[100] = "efghtrd";
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement