Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // returns number of max elements in array
- int count_max_arr(int * arr, int n){
- int i, c = 1;
- int mx = arr[0];
- for (i = 0; i < n; i++){
- if (arr[i] == mx){
- c++;
- } else{
- if (arr[i] > mx){
- mx = arr[i];
- c = 1;
- }
- }
- }
- return c;
- }
- // returns the index of the first occurrence of x in the array
- // -1, if not found
- int first(int * ax, int x, int n){
- int i;
- for(i = 0; i < n; i++){
- if(ax[i] == x){
- return i;
- }
- }
- return -1;
- }
- // returns the index of the last occurrence of x in the array
- // -1, if not found
- int last(int * ax, int x, int n){
- int g = -1, i;
- for(i = 0; i < n; i++){
- if(ax[i] == x){
- g = i;
- }
- }
- return g;
- }
- int len(char * c){ // length of string
- int i;
- for (i = 0; c[i] != '\0'; i++);
- return i;
- }
- // concatenate str and str1, result in str
- char concat(char * str, char * str1){
- int l = len(str), i;
- for(i = 0; str1[i]!='\0'; i++){
- str[l] = str1[i];
- l++;
- }
- str[l] = '\0';
- return str;
- }
- // returns string concatenate str and str1
- char * concat1(char * str, char * str1){
- char * result = malloc(sizeof(str) + sizeof(str1) + 1);
- int l = 0, i;
- for(i = 0; str[i]!='\0'; i++){
- result[l] = str[i];
- l++;
- }
- for(i = 0; str1[i]!='\0'; i++){
- result[l] = str1[i];
- l++;
- }
- return result;
- }
- // counts number of char c in string
- int count_char(char * str, char c){
- int i, k = 0;
- for(i = 0; str[i]!='\0'; i++){
- if(str[i] == c){
- k++;
- }
- }
- return k;
- }
- int count_str_in_str(char *str, char *str1){
- int i, j, k = 0;
- for(i = 0; i < len(str) - len(str1) + 1; i++){
- int f = 1;
- for(j = 0; str1[j]!='\0'; j++){
- if(str[i + j] != str1[j]){
- f = 0;
- }
- }
- if (f == 1){
- k++;
- }
- }
- return k;
- }
- int first_find_str_in_str(char * a, char * b){
- int i, j, s, x = 0;
- for(i = 0; i < len(a) - len(b) + 1; i++){
- s = 1;
- for(j = 0; b[j]!='\0'; j++){
- if(a[i + j] != b[j]){
- s = 0;
- }
- }
- if (s == 1){
- return i;
- }
- }
- return -1;
- }
- // returns string with x elements c
- char * get(int x, char c){
- char * a = malloc(100 * sizeof(char));
- int i;
- for (i = 0; i < x; i++){
- a[i] = c;
- }
- a[i] = '\0';
- return a;
- }
- int main(){
- char a[100] = "abcd";
- char b[100] = "efghtrd";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement