Question
PLEASE WRITE CODE THAT WILL TEST THESE FUNCTIONS IN MAIN, THANK YOU //#9 int find(char *h, char *n) { int x = -1; int c
PLEASE WRITE CODE THAT WILL TEST THESE FUNCTIONS IN MAIN, THANK YOU
//#9
int find(char *h, char *n)
{
int x = -1;
int c = 0;
while(*h != *n && *h != '\0')
{
c++;
h++;
}
if(*h == *n)
{
x =c;
}
return x;
}
//#10
char *ptr_to(char *h, char *n)
{
if(*h == NULL)
{
return NULL;
}
while(*h != '\0' && *h != *n)
{
h++;
}
return h;
}
// #14
int strcmp_ign_case(char *s1, char *s2)
{
char *t = s1;
char *p = s2;
char first = tolower(*t);
char second = tolower(*p);
int result = first - second;
while(result == 0 && *t != '\0' && *p !='\0')
{
first = tolower(*t);
second = tolower(*p);
result = first - second;
t++;
p++;
}
if(*t == '\0' && *p!='\0')
{
return -1;
}
if(*t != '\0' && *p=='\0')
{
return 1;
}
return result;
}
//16
char *dedup(char *s)
{
char *t = s;
char *r, *p, *q;
int i;
int c = 0;
while(*t != '\0')
{
c++;
t++;
}
r = (char *)malloc(c + 1);
q = r;
for(i = 0;i { *q = '\0'; q++; } t = s; p = r; while(*t != '\0') { q = r; while(*q != *t && *q != '\0') { q++; } if(*q == '\0') { *p = *t; p++; t++; } else { t++; } } *p = '\0'; return r; } //18 int ends_with_ignore_case(char *s, char *suff) { int c = 0; char *t = suff; while(*t != '\0') { t++; c++; } t = s; while(*t != '\0') { t++; } t = t -c; char *p = suff; char ignoreSuff = tolower(*p); char ignoreStr = tolower(*t); while(ignoreSuff == ignoreStr && *p !='\0' && *t != '\0') { p++; t++; ignoreSuff = tolower(*p); ignoreStr = tolower(*t); } if(*p == '\0') return 1; else return 0; } //19 char *repeat(char *s, int x) { int c = 0; int i; char *t = s; char *r, *q; while(*t != '\0') { t++; c++; } r = (char*)malloc(c * (x +1)+1); t = s; q = r; for(i=0;i { while(*t != '\0') { *r = *t; t++; r++; } *r = ' '; r++; t = s; } *r = '\0'; return q; } //20 char *replace(char *s, char *pat, char *rep) { char *stringP = s; char *patP = pat; char *repP = rep; char *res, *r, *q; int strLen, patterns, repLen, patLen, i; strLen = repLen = patLen = patterns = 0; while(*stringP != '\0') { strLen++; stringP++; } stringP = s; while(*stringP != '\0') { while(*stringP == *patP && *patP!= '\0' && *stringP != '\0') { patP++; stringP++; } if(*patP == '\0') { patterns++; } if(*stringP =='\0') { break; } if(*stringP != *patP) { stringP++; } patP = pat; } while(*patP != '\0') { patLen++; patP++; } while(*repP != '\0') { repLen++; repP++; } res = (char*) malloc(strLen + (patterns * repLen) -(patterns * patLen)+ 1); r = res; stringP = s; patP = pat; repP = rep; while(*stringP != '\0') { if(*stringP == *patP) { q = res; } while(*patP != '\0' && *patP == *stringP && *stringP != '\0') { *res = *stringP; stringP++; patP++; res++; } if(*patP =='\0') { res = q; while(*repP != '\0') { *res = *repP; repP++; res++; } repP = rep; } if(*stringP != *patP) { *res = *stringP; res++; stringP++; } patP = pat; } *res = '\0'; return r; } //21 char *str_connect(char **strs, int n, char c) { char *str; int i, j, k, len = 0; //Looping over strings present in the array for(i=0; i { //Finding length of current string for(j=0; strs[i][j] != '\0'; j++) len++; } //Allocating enough space str = (char*) malloc(sizeof(char)*(len+n+1)); k=0; //Looping over array elements for(i=0; i { //Consider each string for(j=0; strs[i][j] != '\0'; j++) { //Storing char by char str[k] = strs[i][j]; k++; } //Condition to check for last element of array if(i < n-1) { //Adding concatenation character str[k] = '+'; k++; } } //Adding sting termination character str[k] = '\0'; //Return string return str; } //remove empties void rm_empties(char **words) { int i=0, j, k, len, p = 0; //Loop till all strings are processed while(words[i] != NULL) { len = 0; //Finding length of string for(j=0; words[i][j] != '\0'; j++) len++; //If length is 0 if(len == 0) { k = i; //Removing element by adjusting element of array while(words[k+1] != NULL) { //Swapping words[k] = words[k+1]; k++; } //Incrementing number of elements removed p++; } else i++; } //Adding null pointer words[(i-p)] = NULL; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started