Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- int my_strlen(const char str[]) // Helper function
- {
- int counter = 0;
- while (str[counter] != '\0')
- counter++;
- return counter;
- }
- int my_strcmp(const char str1[], const char str2[], int idx1, int idx2, int size) // Helper function
- {
- for (; size > 0; size--) {
- if (str1[idx1] != str2[idx2]) return 1;
- idx1++; idx2++; }
- return 0
- }
- char* replicate_str(const char* str, int times)
- {
- int n=my_strlen(str), i, j;
- char *ptr;
- ptr = (char*)malloc( ((n*times)+1)*sizeof(char) );
- if(ptr==NULL) return NULL;
- if(n==0 || times==0) free(str);
- return str;
- for(i=0; i<=times; i++) {
- for(j=0; j<n; j++) {
- ptr[(i*n)+j] = str[j];
- ptr[(i*n)+j+1)]= '\0' ; }
- }
- return ptr;
- }
- int* locate_str(const char* str, const char* find, int* numOfHits) {
- int n=my_strlen(str); , k= my_strlen(find); , i, *copy;
- *numOfHits = 0;
- if (n==0 || n < k)
- retutn NULL;
- copy = (int*)malloc((*numOfHits)*sizeof(int));
- if(copy==NULL) {
- return NULL;
- *numOfHits = -1;
- }
- for(i = 0; i < n; i++){
- if(my_strcmp(str,find, i,0,k)==0)
- copy[*numOfHits]=i
- *numOfHits++; }
- if(*numOfHits==0) return NULL;
- return copy;
- }
- char* join_strs(const char* strs[], int numOfStrs, char delimiter)
- {
- int i, n , k , j , total;
- char *join;
- for(i=0; i<=numOfStrs; i++)
- n = n+my_strlen(strs[i]);
- join=(char*)malloc(n+numOfStrs-1);
- for(k=0; k < numOfStrs ;k++) {
- for (j=0; j<my_strlen[k]; j++) {
- join[total]= strs[k][j];
- total++ ;
- }
- if (total!= n- 1) {
- joine[total] = delimiter;
- total++;
- }
- }
- join[total]= '\0';
- return join;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement